Display the Number of Comments by Author

in WordPress Tutorials

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();

Comments (4)

  • Comment by Kavita
    Kavita
    says:

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

  • Comment by Pippin Williamson
    Pippin Williamson
    says:

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

  • Comment by Rev. Voodoo
    Rev. Voodoo
    says:

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

  • Comment by Rev. Voodoo
    Rev. Voodoo
    says:

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