0

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: alt text

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>

1 Answer 1

2

get_comments_number works in the loop. You're using it outside. So do something like this

    $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);
    foreach($posts as $post){
      setup_postdata($post);
          $total = get_comments_number();
          echo 'here we create the HTML and content for the post';
    }
    wp_reset_query();
7
  • I think "numberposts" is deprecated in wp 3 in favor of "posts_per_page" Commented Dec 16, 2010 at 15:58
  • I see what you're saying and I tried that but it doesn't seem to want to echo everything that's contained in the <li>. Nothing shows up. Not sure what I'm doing wrong. Commented Dec 16, 2010 at 17:11
  • @AmbitiousAmoeba it is not deprecated and is actually preferable in get_posts() because of some lingering bug with defaults. There was question recently where that came up.
    – Rarst
    Commented Dec 16, 2010 at 17:29
  • ah yes, I've mistaken it with showposts Commented Dec 16, 2010 at 17:53
  • 1
    @pollyplummer in your code, replace echo $total; with echo $post->comment_count; Commented Dec 17, 2010 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.