Post Pic

Chris Berry asked “How to display the number of comments on each post?”

One of my readers, Chris Berry, recently asked in the forum how he can display the number of comments for each posts. While some themes do it by default, most of them don’t display it at all. Here’s a recipe to solve Chris problem.

To achieve this recipe, simply open your index.php file and paste the following code within the loop:

<?php comments_number('No comments yet','1 comment','% comments')?>

Even better, this simple line of code have a link to jump directly to the comments:

<a href="<?php the_permalink() ?>#comments"><?php comments_number('No comments yet','1 comment','% comments')?></a>

7 Responses

Dec 16 2008 11:20

Hey,

Instead of the last one, try . Much cleaner and it accepts the same parameters as comments_number.

Dec 18 2008 03:00

I have also been using this little code to showoff comments on some of my most popular posts. Thanks for this breakfast recipe.

Dec 22 2008 13:52

Good for themes that don’t have this feature ^_^

Feb 27 2009 18:22

I don’t understand where in the html I should insert that code. Can someone please help?
Thanks!

Apr 13 2009 14:52

Got to implementing this at my blog. Few issues:

1. permalink is excessive I think, link to anchor is enough inside post.

but!

2. if there are no comments yet #comments anchor doesn’t exist and link fails. comments_popup_link function handles this by changing anchor to #respond but it doesn’t work inside single post.

Apr 13 2009 15:02

No promises on code quality but that’s what I ended up with:

<a href="<?php if (0==get_comments_number()) echo '#respond'; else echo '#comments'; ?>" title="Comments"><?php comments_number('Reply', '1 Comment', '% Comments'); ?></a>

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required