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 . '%'; // 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?
We Recommend
https://kinsta.com › wordpress-hosting
Fast and secure infrastructure, worldwide CDN, edge caching, 35 data centers, and enterprise-level features included in all plans. Free site migrations.
https://gravityforms.com › features
Create custom web forms to capture leads, collect payments, automate your workflows, and build your business online. All without ever leaving WordPress.
Leave a Comment