• Resolved geekpowerchris

    (@geekpowerchris)


    Problem, I have one post but it repeats on the blog page.

    Everything was working fine until I added an extra
    if (have_posts())
    My post in the loop repeated.

    Of course you would need more information than that to get a clear picture but I was able to reproduce the problem in a short piece of code.

    while (have_posts())
    {
      the_post(); have_posts();
      if ($i++ == 10)
      {
      	echo "loop";
      	break;
      }
    }

    From what I read about have_posts() it only checks an interal counter so this should not be a problem.

    The most likely reason is a bad install or something is corrupted on my end cause I cannot find anyone else you seems to have the same issue.

    Running xampp on windows xp. PHP 5.2.9. WordPress 2.9.1

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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.

    Thread Starter geekpowerchris

    (@geekpowerchris)

    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 &raquo;'); ?>
    				        </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 &raquo;'); ?>
            </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('&laquo; Older Entries') ?></div>
          <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></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(); ?>

    Thread Starter geekpowerchris

    (@geekpowerchris)

    bah……

    so have_posts() returns false and then rewinds

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘have_posts() returning true when it should be false’ is closed to new replies.