Everything was working fine until I added an extra
if (have_posts())
And there’s your problem. You can’t just use if (have_posts()) a second time unless you’re running multiple loops.
Perhaps it would be better if you described that you’re trying to achieve.
I have read that multiple loops and that is where I read:
The have_posts() simply calls into $wp_query->have_posts() which checks a loop counter to see if there are any posts left in the post array. And the_post() calls $wp_query->the_post() which advances the loop counter and sets up the global $post variable as well as all of the global post data. Once we have exhausted the loop, have_posts() will return false and we are done.
Which gave me the impression that calling have_posts() would not affect the anything. Only the_post() will move the counter forward.
But let me describe what I am doing. I am new to WordPress and I did a lot of reading so I am familiar enough to get myself into this current mess.
For my theme, the index.php page loads a custom header header('blog'); and I use the first post to populate the header.
Before starting the “loop” in the main body of index.php I use the rest of the first post to populate some fields and then advance forward using the_post().
Here is my code:
<?php get_header(); ?>
<?php /* if there is a post add the post title to the header */
get_header('blog'); ?>
<div class="main">
<?php get_sidebar(); ?>
<div class="blog-main-content">
<?php /* if there is a post then its title has already been added to the header. my attempt to fix the have_posts() problem. */
rewind_posts();
if (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="first-entry">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
</div>
/*This part is interesting. I have only one post for my test data and this check returns false as expected*/
<?php if (have_posts()) { ?>
<hr />
<?php } ?>
/*But this check then returns true*/
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<p class="post-date"><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></p>
<div class="entry">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
</div>
<?php if (have_posts()) { ?>
<hr />
<?php } else {
break;
}
?>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
</div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php get_search_form(); ?>
<?php endif; ?>
</div>
</div>
<?php get_footer(); ?>
bah……
so have_posts() returns false and then rewinds