0

I have this function:

<?php $popular = new WP_Query('orderby=comment_count&posts_per_page=5'); ?>
    <?php while ($popular->have_posts()) : $popular->the_post(); ?> 
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li>
<?php endwhile; ?>

Where I added $popular->wp_comment_count, but it did not work at all. How do I do this? I want to let see the comment count per post...

3

Try this:

<?php $popular = new WP_Query('orderby=comment_count&posts_per_page=5'); ?>
<?php while ($popular->have_posts()) : $popular->the_post(); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> has <?php comments_number( 'no responses', 'one response', '% responses' ); ?>.</li>
<?php endwhile; ?>

From the codex.

0
4

There is no wp_comment_count in a WP_Query object, either in the "top" level as in your code-- $popular->wp_comment_count-- or in the individual post objects which is what you actually want to be checking.

There is a comment_count in the post objects though, and that is what you want-- $post->comment_count.

Your code should look like this (cleaned up and formatted into something readable):

while ($popular->have_posts()) : 
  $popular->the_post(); ?> 
  <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li><?php 
  echo $post->comment_count; // here is your count
endwhile;

Notice that you are checking $post for the comment count. $post is a global set for each item in the Loop by $popular->the_post(). If you are trying to display the count, as opposed to do some other operations based on the comment count, you should probably use comments_number as in the answer by @NathanPowell

0

Use my way

<?php
$commentscount = get_comments_number();
if($commentscount == 1): $commenttext = 'comment'; endif;
if($commentscount > 1 || $commentscount == 0): $commenttext = 'comments'; endif;
echo $commentscount.' '.$commenttext;
?>

its working in all version.

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