Some days ago, I shown you a great hack about getting the total number of posts published on your WordPress blog. Today, let’s do the same with comments.
Some days ago, I shown you a great hack about getting the total number of posts published on your WordPress blog. Today, let’s do the same with comments.
This simple hack works exactly as the "get total number of posts" hack works: We're using the $wpdb object and make a custom query to MySQL:
<?php
$numcomms = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1'");
if (0 < $numcomms) $numcomms = number_format($numcomms);
?>
Right now, the $numcomms variable contains the total number of comments posted on your WordPress blog. To display this number, simply do something like:
<?php echo "There's ".$numcomms." comments on this blog";
10 Responses
Use wp_count_comments() for that. It returns an Array with approved, moderated, spam, total_comments
Here is a post with some expamples.
I didn’t knew that function, thanks!
You need to close the php tags in the second textarea.
@Paul Davis: That’s true, I’ll correct it soon. Thanks for letting me know!
I can’t find this function – wp_count_comments() in Wordpress documentation and it doesn’t work on my blog, or maybe I did something wrong. I have WP 2.8.4
Hallo agin, I found a very simple plugin for this – Comment Count by Nick Momrik. Instalation: only activate the plugin and place mdv_comment_count() in template. It counts only approved comments. It works with WP 2.8.4
Trackbacks: