Display the Total Number of Comments on Your WordPress Blog

by Hiroshi on November 16, 2008

in Hacks, WordPress

In this tutorial we will learn how to show total number of comments at your blog using the $wpdb object. Use the following code where you want to display total number of comments at your blog.

<?php
$numcomms = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1'");
if (0 < $numcomms) $numcomms = number_format($numcomms);
?>

Here $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." total comments on my blog"; ?>

You can combine the code as well like following:

<?php
$numcomms = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1'");
if (0 < $numcomms) $numcomms = number_format($numcomms);
echo "There's ".$numcomms." total comments on my blog";
?>

This should show you that how many comments your blog has got.

Related Posts

Previous post:

Next post: