I have an if/else to call the template for a post type, and for some reason I cannot seem to figure out why the first portion is stuck in an infinite loop.
Here's the main page loop:
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<?php if ( has_term('bands', 'work_family')):
get_template_part('content', 'single-bands');
else:
get_template_part('content', 'single-work');
endif; ?>
<?php endwhile; endif; ?>
I'm pretty sure the problem is in the single-bands template that is getting loaded, because if I comment out that line and stick some text in there it works just as it should. Here's that template:
<header class="fl-post-header">
<h1 class="fl-post-title" itemprop="headline">
<?php
// strip "Songs by" from titles for archive page
$title = get_the_title();
$prefix = 'Songs by ';
$stripper = $title;
if (substr($stripper, 0, strlen($prefix)) == $prefix) {
$stripper = substr($stripper, strlen($prefix));
}
?>
<?php echo $stripper; ?>
<?php edit_post_link( _x( 'Edit', 'Edit post link text.', 'fl-automator' ) ); ?>
TEMPLATE LOADED CORRECTLY IF YOU SEE THIS
</h1>
</header><!-- .fl-post-header -->
<div class="fl-content <?php FLTheme::content_class(); ?>">
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<article <?php post_class( 'work-post' ); ?> id="fl-post-<?php the_ID(); ?>" itemscope="itemscope">
<?php the_content(); ?>
<?php get_template_part('content', 'band-performances') ?>
<?php endwhile; endif; ?>
</div>
Nothing else loop related on the page and all the html works fine when I load the template directly instead of in that outer loop from the other page. I'm relatively certain the problem is in the header, because the rest of the template does not get looped, just that.
Related
I've stuck in dev WP theme, please help me.
The problem is when I wrote code for displaying post in front-page.php, it display me the_title() and the_content() from page, not from post.
There is code:
<?php if(have_posts()): ?>
<?php while(have_posts()) : the_post(); ?>
<h2 class="blog-post-title"><?php the_title( ); ?> </h2>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p><?php __('No post found') ?></p>
<?php endif; ?>
It displays me post only if I go to post location e.g. localhost/2017/10/16/first-post/.
There is picture how it correctly shows but on another location.
http://prntscr.com/gy922y
Since you are displaying in a custom page, You'll need to declare and specify post_type as post
Here is a working example:
<?php
$args = array(
'post_type' => 'post'
);
$post_query = new WP_Query($args);
if($post_query->have_posts() ) {
while($post_query->have_posts() ) {
$post_query->the_post();
?>
<h2 class="blog-post-title"><?php the_title( ); ?> </h2>
<?php the_content(); ?>
<?php
}
}
?>
I am trying to get latest posts to display through a template page i am building for pages, the loop is not running the latest post only one page
ok, I have a simple loop that gets latest post
my loop
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content', get_post_format());
endwhile; endif;
?>
and content.php
<div class="blog-post">
<h2 class="blog-post-title">
<?php the_title(); ?>
</h2>
<p class="blog-post-meta">
<?php the_date(); ?>by <?php the_author(); ?>
<a href="<?php comments_link(); ?>">
<?php printf(_nx('One Comment', '%1$s Comments', get_comments_number(), 'comments title', 'textdomain'), number_format_i18n(get_comments_number())); ?>
</a>
</p>
<?php if ( has_post_thumbnail() ) {?>
<div class="row">
<div class="col-md-4">
<?php the_post_thumbnail('thumbnail'); ?>
</div>
<div class="col-md-6">
<?php the_excerpt(); ?>
</div>
</div>
<?php } else { ?>
<?php the_excerpt(); ?>
<?php } ?>
</div>
when i run the loop in index.php i get my latest blog post, perfect.
however, i am building a template page, i try and include the loop in this page, i just get one page (not all posts).
my template
<div class="row">
<div class="col-sm-12">
// content bar
<?php get_template_part('advicecentre_bar', get_post_format()) ?>
// cmd driven content
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content_page', get_post_format());
endwhile; endif;
?>
// recent post
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content', get_post_format());
endwhile; endif;
?>
</div> <!-- /.col -->
</div> <!-- /.row -->
<?php get_footer(); ?>
If you are using multiple loops on the same page, you must use rewind_posts() like so:
<div class="row">
<div class="col-sm-12">
// content bar
<?php get_template_part('advicecentre_bar', get_post_format()); ?>
// cmd driven content
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content_page', get_post_format());
endwhile; endif;
?>
<?php rewind_posts(); ?>
// recent post
<?php
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content', get_post_format());
endwhile; endif;
?>
</div> <!-- /.col -->
</div> <!-- /.row -->
<?php get_footer(); ?>
This "resets" the loop to it's original state and allows you to look through the posts again. In your original code you scan through all the posts, and then in your second loop scan through nothing, as you have already scanned through all the posts!
Hmm I have found this solution using a for each rather than the while loop seems to work, but im not sure if its the best way around.
<ul>
<?php
$recent_posts = wp_get_recent_posts();
foreach( $recent_posts as $recent ){
echo '<li>' . $recent["post_title"].' </li> ';
}
wp_reset_query();
?>
</ul>
UPDATE
<?php
$args = array('numberposts' => 5);
$recent_posts = wp_get_recent_posts($args);
foreach ($recent_posts as $recent) {
$excerpt = wp_trim_excerpt($recent['post_content']);
$permalink = get_permalink($recent["ID"]);
$title = esc_attr($recent["post_title"]);
$thumbnail = get_the_post_thumbnail($recent["ID"], 'thumbnail');
echo '<li><a href="' . $permalink . '" title="Look ' . $title . '" >' . $thumbnail . $title . '</a></li>';
echo $excerpt;
}
?>
I am building a custom theme from scratch and come across a slight problem that I need some help with.
So I have on my front-page a list of the 3 latest blog posts showing the 'title', 'Excerpt' & a 'more...' link which both the title and more link take you to single.php.
I am generating the post content in a file named 'content-post.php' which is the following:
<div class="clearfix">
<?php echo get_the_post_thumbnail( $post_id, $size, $attr ); ?>
<header class="title">
<h3><?php the_title(); ?></h3>
</header>
<ul class="info">
<li><?php the_category(', '); ?> | <?php the_time('F Y'); ?></li>
<!--<li>Written By: <a href="<?php bloginfo('siteurl') ?>/about/"><?php the_author(); ?></li>-->
</ul>
<div class="excerpt">
<p><?php if(is_single()): ?>
<?php the_content(); ?>
<?php comments_template(); ?>
<?php else: ?>
<?php the_excerpt(); ?>
<a class="post-link" href="<?php the_permalink(); ?>">More...</a></p>
<?php endif; ?>
</div>
</div>
This builds the posts out to front-page.php just fine. I am having the problem when you go into the blog page which is using the same post content and the layout is the same. Is there a way I can specify how it shows on the front-page and how it appears on the blog page?
The post is being displayed on front-page.php like this:
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', 'post' ); ?>
<?php endwhile; endif; ?>
And on the blog page like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'post' ); ?>
<?php endwhile; else: ?>
<p>There are no posts to display or pages here</p>
<?php endif; ?>
If I understand your question, try this:
<?php if (is_page(page id)){your query}
Good luck! ;)
PS.. or on your way:
<?php if (is_page(page id)): ?>
your query
<?php endif; ?>
I have a set-up a custom theme on my wordpress install. I have been developing with it locally and everything was working fine.
I uploaded to my server tonight and was setting up the fresh wordpress install. I turned on permalinks and all of a sudden my custom category pages are causing infinite re-direct loops.
My .htaccess is writable so I don't think its that problem (I have seen this mentioned a lot online).
The code from one of my custom pages is below - it pulls from a specific category - does anyone know how to fix this issue?
<?php get_header(); ?>
<?php
/*
Template Name: Podcasts
*/
?>
<ul class="mcol">
<?php
query_posts("cat=1");
while(have_posts()) : the_post(); ?>
<li class="article" id="post-<?php the_ID(); ?>">
<?php
if ( has_post_thumbnail() ) { ?>
<?php
$imgsrcparam = array(
'alt' => trim(strip_tags( $post- >post_excerpt )),
'title' => trim(strip_tags( $post->post_title )),
);
$thumbID = get_the_post_thumbnail( $post->ID, array(200,200), $imgsrcparam ); ?>
<div class="preview"><?php echo "$thumbID"; ?><div class="front-titles"><?php the_title(); ?></div></div>
<?php } else {?>
<div class="preview"><img src="<?php bloginfo('template_url'); ?>/images/default-thumbnail.jpg" alt="<?php the_title(); ?>" /></div>
<?php } ?>
<div class="article-over">
</div>
</li> <?php ?>
<?php endwhile;
//Reset Query
wp_reset_query();
?>
</ul>
<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<?php endwhile; ?>
<?php else : ?>
<h1 id="error"><?php _e("Sorry, but you are looking for something that isn’t here."); ?></h1>
<?php endif; ?>
<div class="pagination"><?php previous_posts_link('<< Newer Entries', 0) ?> <?php next_posts_link('Older Entries >>', 0); ?> </div>
<?php get_footer(); ?>
Anyone any ideas? I'm quite new to this so am unsure as to what the problem might be...
What your doing is weird.
query_posts is meant to alter the main loop.
You have 2 loops with different syntax on the same page.
The second loop is not doing anything, so remove it, and use get_posts or WP Query for your custom query.
I am trying to create a single php file I can use to display the content for the index or a page in wordpress. This question is not wordpress specific though.
I have a 'while' which needs to be closed in two different places depending on whether the index or a page is being displayed. I am trying to close the 'while' with an 'if'
if (is_home()) { endwhile; }
// otherphp
if (is_page()) { endwhile; }
This is not working though. The full code is below:
<section id="main">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if (is_home()) : ?> <article <?php post_class() ?> id="post-<?php the_ID(); ?>"> <?php elseif (is_page()) : ?> <article class="post" id="post-<?php the_ID(); ?>"> <?php else : NULL; endif; ?>
<div class="entry">
<?php the_content(); ?>
</div>
</article>
<?php if (is_home()) { endwhile; } ?> <!-- ENDWHILE MUST BE HERE ON INDEX PAGE -->
<?php if (is_home()) : get_template_part('nav','default'); elseif (is_page()) : NULL; else : NULL; endif; ?>
<?php if (is_page()) { endwhile; } ?> <!-- ENDWHILE MUST BE HERE ON ALL OTHER PAGES -->
<?php endif; ?>
</section>
I realise this code is somewhat inefficient and messy but at the moment I am not concentrating on efficiency. I am simply trying to make it work.
How can I conditionally end the 'while'?
Just do a normal condition inside a normal while:
while (/* something */) :
// same for all
if (/* is home */) {
// do something only for home
}
endwhile;