Display the Number of Comments by Author

Written by on May 6, 2011 in WordPress Tutorials - 5 Comments

A really cool feature that some sites, such as Pro Blog Design, utilize is showing the number of comments an author has posted next to their name and gravatar in a post’s comment list. This is a function that should really be included in the WP core, but, alas it’s not. But no worry because we’ll just make our own.

Copy the function below into your functions.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function author_comment_count(){
 
	$oneText = '1';
	$moreText = '%';
 
	global $wpdb;
 
	$result = $wpdb->get_var('
		SELECT
			COUNT(comment_ID)
		FROM
			'.$wpdb->comments.'
		WHERE
			comment_author_email = "'.get_comment_author_email().'"'
	);
 
	if($result == 1): 
 
		echo str_replace('%', $result, $oneText);
 
	elseif($result > 1): 
 
		echo str_replace('%', $result, $moreText);
 
	endif;
 
}

To display an author’s total number of comments, use the function like this inside of your comments loop:

1
author_comment_count();

About

Pippin Williamson is an expert WordPress dev with 4+ years of experience. You may follow him on Twitter @pippinspages and @pippinsplugins and see his free WP plugins, themes, and tutorials he has to offer at Pippin's Pages.com and Pippin's Plugins.com

Post comment as twitter logo facebook logo
Sort: Newest | Oldest

Thanks for posting the code for showing how many comments are on the post. Sure useful if u are designing templates to display in a cloud or whatever is designed.

Just got the right code I was looking for. Thanks for share

Sweet, thanks for another bit of code I didn't know I wanted!

Oops, didn't notice the html entities there, looks like a couple of > that need converted to > for the code to work.

Oh whoops, you're completely right. I've updated it.