How To Display Author’s Comment Count In WordPress Without A Plugin

My featured WordPress trick for today is from freelance Web Designer Boutros AbiChedid, and it’s basically a way for you to display the author’s number of comments, or comment count, WITHOUT a plugin.

ScreenHunter 80 Aug. 24 09.21 How To Display Author’s Comment Count In WordPress Without A PluginAs you can see, the number is displayed right next to the author’s name and also is basically the total number of comments made throughout the whole site, and not just the particular post.

Like with most WordPress hacks, all it takes is adding a code onto the functions.php file.

Copy/paste the code below:

<?php

/************************************CODE-B*********************************

* @Author: Boutros AbiChedid

* @Date:   July 2, 2012

* @Websites: bacsoftwareconsulting.com/blog/ ; blueoliveonline.com/

* @Description: Add the Comment count per Author based on his/her Email.

* @Tested on: WordPress version 3.4.1

* If you find this code helpful, please donate to keep new WP codes coming.

* @DO NOT REMOVE THIS SECTION (see terms of use).

****************************************************************************/
function  bac_comment_count_per_user() {

global $wpdb;

$comment_count = $wpdb->get_var(

'SELECT COUNT(comment_ID) FROM '. $wpdb->comments. '

WHERE comment_author_email = "' . get_comment_author_email() .'"

AND comment_approved = "1"

AND comment_type NOT IN ("pingback", "trackback")'

);
//Discriminate between singular and plural.

if ( $comment_count == 1) {

echo ' (1 comment)';

}

else {

echo ' (' . $comment_count . ' comments)';

}

}

?>

Displaying the comment count depends on the theme that you are using. There are three scenarios.

Case1: The wp_list_comments() function with a callback parameter. Something like:

<?php wp_list_comments('callback=mytheme_comment'); ?>

In this case, you need to edit the mytheme_comment function located in your theme’s functions.php file. You can see an example of how to do this on Boutros’ page which I’m going to provide you a link to below this post.

Case2: The wp_list_comments() function with no callback. Something like:

<?php wp_list_comments(); ?>

The default WordPress wp_list_comments function is used in this scenario. In this case, you need to add a custom callback function as a parameter to the wp_list_comments() function. You then need to define this callback in the functions.php file.

Case3: Nothing’s similar to Case1 and Case2 except the old way of listing the comments, which indicates that your theme is very old.

According to Boutros, if you are using such a theme, you ought to either upgrade or change it. Otherwise, you need to modify the code in the functions.php file. He also gives a few more notes regarding the usage of this hack like CSS styling, Case1 examples, and its disadvantages in his full article HERE.

Incoming search terms for the article:

Related Posts

How to Create a Clean and Colorful Web Layout

Create An Instagram Widget In Photoshop

How to Create an Appealing Coming Soon Page

How to Create Text with Flaming Meteor Effect