I am using this code to show related posts without a plugin. Each post shows the title and comment count. The end result is similar to the way Mashable does it:
This appears under each post on single.php. My problem is that the comment count shows the same number on each - it shows the number of comments for the current post. I've tried it a couple different ways as you can see below. How can I change it so that it will show the comment number for each post? By the way, this only started happening recently - previously it showed comment count for each. How weird is that?!
<div class="relatedrandom">
<h3>You May Also Like ...</h3>
<ul>
<?php
$this_post = $post;
$total = get_comments_number();
$category = get_the_category(); $category = $category[0]; $category = $category->cat_ID;
$posts = get_posts('numberposts=5&offset=0&orderby=post_date&order=DESC&category='.$category);
$count = 0;
foreach ( $posts as $post ) {
if ( $post->ID == $this_post->ID || $count == 4 ) {
unset($posts[$count]);
}else{
$count ++;
}
}
?>
<?php foreach ( $posts as $post ) :
?>
<li>
<div class="firstimage"><?php get_first_image($post->ID); ?></div>
<p><a href="<?php the_permalink(); ?>" class="purpleLink"><?php the_title(); ?></a></p>
<span class="commentbubble"><a href="<?php the_permalink(); ?>"> <?php echo $total; ?> <img src="http://mydomain.com/greybubble.jpg" /></a></span>
</li>
<?php endforeach // $posts as $post ?>
</ul>
<?php
$post = $this_post;
unset($this_post);
?>
</div>
<div class="relatedrandom">
<h3>From the Archives...</h3>
<ul>
<?php
$rand_posts = get_posts('numberposts=4&orderby=rand');
foreach( $rand_posts as $post ) :
?>
<li>
<div class="firstimage"><?php get_first_image($post->ID); ?></div>
<p><a href="<?php the_permalink(); ?>" class="purpleLink">
<?php the_title(); ?></a></p>
<span class="commentbubble"><a href="<?php the_permalink(); ?>"><?php comments_number('0','1','%'); ?> <img src="http://mydomain.com/greybubble.jpg" /></a></span>
</li>
<?php endforeach; wp_reset_query(); ?>
</ul>
</div>