Title: Using $post in foreach loop breaks comments on single.php
Last modified: August 19, 2016

---

# Using $post in foreach loop breaks comments on single.php

 *  [echeats](https://wordpress.org/support/users/echeats/)
 * (@echeats)
 * [17 years, 10 months ago](https://wordpress.org/support/topic/using-post-in-foreach-loop-breaks-comments-on-singlephp/)
 * Following on from here:
 * [http://wordpress.org/support/topic/200913?replies=3](http://wordpress.org/support/topic/200913?replies=3)
 * It turns out this isn’t resolved, as when i rename $post to $thepost it fixes
   the comments but breaks the displaying of the_title(), the_permalink() and the_title_attribute();
 * Here is my code:
 *     ```
       <div class="MoreCat">
       		<?php
       		$categories = get_the_category();
       		foreach ($categories as $category) :
       		?>
       		<h3>Similar recent articles</h3>
       			<?php
       			$posts = get_posts('numberposts=5&category='. $category->term_id);
       				foreach($posts as $post) :
       				$values = get_post_meta($post->ID, "Image", true);
       			if (isset($values[0])) { ?>
       			<div class="MoreCatImg">
       			<img src="<?php bloginfo('template_url'); ?>/scripts/timthumb.php?src=<?php echo $values; ?>&amp;w=102&amp;h=102&amp;zc=1&amp;q=85" alt="<?php the_title(); ?>" />
       			<div class="MoreCatText">
       			<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
       			</div>
       			</div>
       		<?php } ?>
       		<?php endforeach; ?>
       		<?php endforeach; ?>
       		</div>
       ```
   

Viewing 1 replies (of 1 total)

 *  [dttn](https://wordpress.org/support/users/dttn/)
 * (@dttn)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/using-post-in-foreach-loop-breaks-comments-on-singlephp/#post-848487)
 * I added
 *     ```
       <?php foreach($posts as $post) :?>
       <?php endforeach; ?>
       ```
   
 * and everything works the it should.
 * this is the code I use
 *     ```
       <?php foreach($myposts as $post) :?>
        <ul><li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li></ul>
       <?php endforeach; ?>
       <?php foreach($posts as $post) :?>
       <?php endforeach; ?>
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Using $post in foreach loop breaks comments on single.php’ is closed 
to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [dttn](https://wordpress.org/support/users/dttn/)
 * Last activity: [17 years, 6 months ago](https://wordpress.org/support/topic/using-post-in-foreach-loop-breaks-comments-on-singlephp/#post-848487)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
