Tutorials WordPress

A Post’s Comment Percentage Compared to the Entire Site

Updated

Written by

Dave Warfel

Reading Time

2 minutes

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

We previously showed you how to display the total number of comments on your WordPress site. Now let’s use that information, along with a single post’s comment count, and find the percentage of total comments that one particular post makes up.

The language can be a bit confusing, so here’s an example of what we’re going to achieve.

  • You have a total of 100 comments across many posts on your entire WordPress blog
  • You have one popular post that has received 10 comments
  • On that one popular post, you want to show your readers that 10% of all your site’s comments are on this one particular post

One of our readers, Martin, is using this on his site (I asked him to share his example).

Get Comment Count for a WordPress Post

We’ll use the get_comments_number function to retrieve a numeric value for total number of comments on the single post, and store it in a variable.

<?php // Get number of comments on the current, single post
$single_post_comments = get_comments_number();
?>

get_comments_number must be used within The Loop.

Get Total Number of Comments on Entire Site

Now let’s get the total number of comments for the entire WordPress site, and store that in another variable.

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

Combine Them (with some math) to Get The Percentage

We combine the two, and use some math to get a percentage. Then store that in another variable.

$comment_percentage = $single_post_comments / $total_comments * 100;

Now let’s put all of this code together, and display the resulting comment percentage on our site.

Display Comment Percentage on WordPress Site

<?php
$count_comments = wp_count_comments();
$total_comments = $count_comments->approved; // Gets total approved comments on site
$single_post_comments = get_comments_number(); // Gets comments on single post
$comment_percentage = $single_post_comments / $total_comments * 100; // Divides the two, and formats into a percentage
echo $comment_percentage . '&#37;'; // Displays the percentage on your page, along with a percent sign
?>

Get Creative

You could forgo displaying the percent sign, and instead just use the resulting number to write some inline CSS. You could represent the percentage with a progress bar that adjusts it’s width or height based on the percentage.

Please keep your inline CSS to an absolute minimum. No more than just a percentage width or height. Styles belong in separate stylesheets, but there are very few exceptions where I think it’s OK to use inline CSS.

How are you using comment percentages in WordPress?

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. 🏔️🏃🚴🤸

Leave a Comment