Title: Adding if statement causing endless loop?
Last modified: August 18, 2016

---

# Adding if statement causing endless loop?

 *  [merlikdotnet](https://wordpress.org/support/users/merlikdotnet/)
 * (@merlikdotnet)
 * [20 years, 3 months ago](https://wordpress.org/support/topic/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)

 *  [onseduction](https://wordpress.org/support/users/onseduction/)
 * (@onseduction)
 * [20 years, 3 months ago](https://wordpress.org/support/topic/adding-if-statement-causing-endless-loop/#post-341813)
 * I don’t understand why you need to have this inside loop if you only need to 
   have this line.
 *  Thread Starter [merlikdotnet](https://wordpress.org/support/users/merlikdotnet/)
 * (@merlikdotnet)
 * [20 years, 3 months ago](https://wordpress.org/support/topic/adding-if-statement-causing-endless-loop/#post-341814)
 * 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…
 *  [moshu](https://wordpress.org/support/users/moshu/)
 * (@moshu)
 * [20 years, 3 months ago](https://wordpress.org/support/topic/adding-if-statement-causing-endless-loop/#post-341815)
 * 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.
 *  [bob58](https://wordpress.org/support/users/bob58/)
 * (@bob58)
 * [20 years, 3 months ago](https://wordpress.org/support/topic/adding-if-statement-causing-endless-loop/#post-341817)
 * 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.
 *  Thread Starter [merlikdotnet](https://wordpress.org/support/users/merlikdotnet/)
 * (@merlikdotnet)
 * [20 years, 3 months ago](https://wordpress.org/support/topic/adding-if-statement-causing-endless-loop/#post-341818)
 * Awesome, that worked. Thanks.
 * Although, if anyone wants to answer, I’d still like to know why what I did messed
   it up.
 *  [bob58](https://wordpress.org/support/users/bob58/)
 * (@bob58)
 * [20 years, 3 months ago](https://wordpress.org/support/topic/adding-if-statement-causing-endless-loop/#post-341820)
 * 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.

## Tags

 * [endless](https://wordpress.org/support/topic-tag/endless/)
 * [have_posts()](https://wordpress.org/support/topic-tag/have_posts/)
 * [if](https://wordpress.org/support/topic-tag/if/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 4 participants
 * Last reply from: [bob58](https://wordpress.org/support/users/bob58/)
 * Last activity: [20 years, 3 months ago](https://wordpress.org/support/topic/adding-if-statement-causing-endless-loop/#post-341820)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
