Displaying the comment count for a specific post in Wordpress
Tuesday, January 27th, 2009 by Peter ZhangPhew, that was a long title. Anyway, to say it short, this trick helps you to display the count of the comment, such as “comment 1, comment 2, etc.”
I was trying to implement that in the new Dark Autumn theme, but my search did not return any helpful results. So I tried to do it myself, and I did it!
If you are using WP 2.7 or greater and uses the new wp_list_comments function to display the comments, please refer to the codex article to setup a custom way to display the comments. If you are not using WP 2.7 or the wp_list_comments function, you may skip this.
Open comments.php in your template folder. If you use wp_list_comments, in the functon you defined, put this in the beginning of the function. If you don’t, put this after the beginning of comment_loop, inside the after <?php foreach ($comments as $comment) : ?>
<?php global $i;
if($i){ $i++; } else { $i = 1; } ?>
Then, put this code at where you want to display the number. It will return a number (1, 2, 15, 743, 8532943, etc.)
<?php echo $i; ?>
How does it work? It is pretty simple actually. At the beginning, we check if variable $i is defined. If it is, then we add 1 to the current number. If it isn’t (which it won’t be at he beginning), then we will make it one. Because this function will run when each time a comment is displayed, it works just about perfectly =)
Feel free to post any questions!






