Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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...

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
Thanks, worked! –  Johan Jul 27 '13 at 16:27

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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