如何在相关帖子上显示评论计数?

时间:2010-12-16 作者:pollyplummer

我正在使用此代码显示没有插件的相关帖子。每篇文章都会显示标题和评论数。最终结果与Mashable的实现方式类似:alt text

这会出现在单张的每篇文章下面。php。我的问题是,每个帖子的评论数都是相同的——它显示了当前帖子的评论数。我尝试了几种不同的方法,正如你在下面看到的。我如何更改它,以便它显示每个帖子的评论编号?顺便说一句,这只是最近才开始发生的——之前它显示了每个人的评论数。这有多奇怪?!

 <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 个回复
SO网友:Cristian Dumitru Antohe

get\\u comments\\u 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);
    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();

结束

相关推荐