WordPress.org

Ready to get started?Download WordPress

Forums

Adding if statement causing endless loop? (7 posts)

  1. Merlikdotnet
    Member
    Posted 8 years ago #

    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?

  2. OnSeduction
    Member
    Posted 8 years ago #

    I don't understand why you need to have this inside loop if you only need to have this line.

  3. Merlikdotnet
    Member
    Posted 8 years ago #

    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...

  4. moshu
    Member
    Posted 8 years ago #

    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.

  5. bob58
    Member
    Posted 8 years ago #

    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.

  6. Merlikdotnet
    Member
    Posted 8 years ago #

    Awesome, that worked. Thanks.

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

  7. bob58
    Member
    Posted 8 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic