Tutorials WordPress

Display Total Number of WordPress Comments

Updated

Written by

Dave Warfel

Reading Time

1 minute

If you buy something from one of our links, we may earn a commission.

Do you want to display the total number of WordPress comments on your entire site? Use the following code to get the total number of comments, and then display it in your theme. This only gets comments with “Approved” status.

<?php // Get total number of approved comments
	$count_comments = wp_count_comments();
	$total_comments = $count_comments->approved;
	echo $total_comments . ' comments. ';
?>

The $total_comments variable will just return the number. You can change the word “comments” to say whatever you want.

I’d love to see how you use this. Please share in the comments.

Reference wp_count_comments() in the WordPress Codex.

Dave Warfel

LinkedIn  •  X (Twitter)
Dave has been working with WordPress since 2011. He's built 100s of client sites and almost a dozen of his own. He's tested almost every plugin you can think of, hosted with at least 10 different companies, and gone down every SEO rabbit hole you can imagine. When's he's not tinkering with new software, you'll find him in the mountains of Colorado, trail running, summiting peaks, and rippin' downhills on his mountain bike. 🏔️🏃🚴🤸

2 responses to “Display Total Number of WordPress Comments”

  1. Martin Avatar
    Martin

    Thanks! I used this code and combined it so to get my percentage for a specific post. After that I used ingraphic code to return my value and added shortcode. Now I have a dynamic progress bar!!! Although it is working I do ask myself if I did it the best way:
    $the_percentage = get_comments_number($the_ID) / $total_comments * 100;

    1. Dave Warfel Avatar

      Martin,

      Interesting idea. Could you please provide a link to your site so I (and other readers) can check it out? I’m curious to see the progress bar.

      I don’t see any issues with going about the code that way. I would have done the same thing. However, 2 things important to mention:

      1. get_comments_number must be used within The Loop.
      2. I think you made a syntax error. For get_comments_number, if you want to get the comments for the post you are on, you can just do this: get_comments_number(). I think $the_ID is an invalid parameter. You were probably thinking $post->ID, which passes the ID of the current post, which is the same thing as just writing it like this: get_comments_number().

Leave a Comment