Support » Fixing WordPress » Adding if statement causing endless loop?

  • I’m trying to modify a theme so that it only adds a horizontal rule between posts. I’m trying to do it like this:

    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php require(‘post.php’); ?>
    <?php if (have_posts()) echo ‘<div class=”line”></div>’; ?> // problem
    <?php endwhile; ?>

    For whatever reason, if the inside if statement is commented out, I have no problems. When it’s uncommented, the loop runs forever, and my blog starts repeating itself. Does the have_posts() function somehow increment back around to the first post? This is really confusing me. What do I need to change?

Viewing 6 replies - 1 through 6 (of 6 total)
  • I don’t understand why you need to have this inside loop if you only need to have this line.

    One line between every post. I don’t want one before the first post or after the last post, so styling won’t work because it’ll cause one of those to happen. I can’t think of any other way to do it…

    I found this somewhere in the forum (I can’t remember where exactly) and it works for me:
    <?php if (($wp_query->current_post +1) != ($wp_query->post_count)) { ?>
    <img src="<?php bloginfo('stylesheet_directory'); ?>/images/nwsep.jpg" width="100" height="20" />
    <? } ?>

    Put it just above the “endwhile” statement in the Loop… and replace the /directory/filename + the size etc.

    I’m not sure, if have_posts() resets the loop (that could make sense), but the information you get is if there are posts in the system. So if there is at least one post available, you will allways get true.
    Or in other words: that won’t work anyway …
    Edit: My answer refers to the original problem. Moshu’s idea works of course.

    Awesome, that worked. Thanks.

    Although, if anyone wants to answer, I’d still like to know why what I did messed it up.

    I had a look into the code …

    If the last post (to display) is reached, have_posts() rewinds to the first post. And I was wrong: If the last post is not yet reached, you get the information if another post is to be displayed.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Adding if statement causing endless loop?’ is closed to new replies.