Display the total number of comments on your WordPress

October 21, 2008 · Print This Article

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:
$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";


Your choice for site templates and wordpress themes

Comments

Got something to say?

You must be logged in to post a comment.