In this tutorial, I will show you how to display the number of comments an author has posted in the post´s comment section without the need of a plugin, as shown in the image below:
Everytime someone leaves a comment on any post, the Author´s comment total count will show up next to his/her name.
Here are the features of the code:
- Only approved comments are counted. Pending (and trashed) comments are NOT counted.
- Code is tested to work properly on WordPress Version 3.4.1, but it will work on earlier versions.
- The code count Authors´ Comments based on their email. Even unregistered users comments are counted.
- There is still an issue with the code: The code still add a “number” for pingbacks/trackbacks. I said a “number”, because this number is some constant and is not an accurate count. Pingbacks/Trackbacks should NOT be counted. There is No email address associated with them. More on that later…
Here is How to display the number of comments by author.
STEP1: Counting Author Comments | functions.php
Open functions.php file located in your theme´s folder, and add (copy and paste) the following CODE-B. Save the file and upload it to the server.
CODE-B: Count Number of Comments per Author
WordPress provides a class of functions for all database manipulations. WordPress has the global variable $wpdb, which is an instantiation of the class already set up to interact with the database. Always use the global $wpdb variable. Remember to globalize $wpdb before using it in any custom functions.
The $wpdb object can talk to any number of tables, but only to one WordPress database.
The get_var function returns a single variable from the database. Though only one variable is returned, the entire result of the query is cached for later use. The query returns NULL if no result is found.
Line 17 of the code, means that only approved comments are counted.
Line 18 of the code, means that Trackback/Pingback comments should NOT counted.
Line 18, does not work. I am not sure why! More on that later…
Thanks to Pontus for all his help in trying to get the code to work.
STEP2: Display the Comment Count
After you added CODE-B to your theme´s functions.php, now you need to display the comment count.
To do that, there are 3 scenarios. It all depends on the theme you are using:
Open your theme´s comments.php file. If in this file you found…
- Case1: The wp_list_comments() function with a callback parameter. Something like:
<?php wp_list_comments('callback=mytheme_comment'); ?>
Then in this case you need to edit the mytheme_comment function located in your theme´s functions.php file. I will show you an example on how to do that in the next section.
- Case2: The wp_list_comments() function with no callback. Something like:
<?php wp_list_comments(); ?>
Then in this case, the default WordPress wp_list_comments function is used. In this case, you need to add a custom callback function as a parameter to the wp_list_comments() function. Then you need to define this callback function in the functions.php file. If you need help, feel free to ask. I will show you how to do that with a donation to this blog. Your donation helps keeping new code snippets coming.
- Case3: You find nothing similar to Case1 or Case2, but the old way of listing the comments. This is an indication that your theme is very old. If you are using such a theme, either upgrade it or change it. Otherwise, you need to modify the code in comments.php file. Again if you need help, feel free to ask. I will show you how to do that with a donation to this blog.
Real Example of (Case1:) | Diary Theme
To give you an example. One of the WordPress themes I am using for this blog is the modified version of the Diary theme. Therefore, I will be using the Diary theme as an example.
The Diary theme is using Case1: meaning that when I checked the comments.php file and looked for the wp_list_comments(). I found out that this function uses the ´mytheme_comment´ as a callback parameter. This is great since now all I have to do is modify the mytheme_comment function located in functions.php file.
Next, I opened functions.php and located the mytheme_comment() function and modify it as follows:
MODIFIED: Display Number of Comments per Author
The mytheme_comment function in the code above, is taken AS IS from the Diary Theme, with the exception of Line 9.
This is the only Line of code needed to modify this function in order to display the total number of Comments per Author. How simple is that!
Styling CODE-B | style.css
To style CODE-B with CSS, you don´t have to do anything extra unless you want to. It will take the same styling as your Comment´s Section. In my case, I did not do anything extra.
Disadvantage of CODE-B
As I mentioned before, CODE-B does NOT ignore trackbacks and pingbacks. The code still add a number for trackbacks and pingbacks. Line 18 of CODE-B does not seem to work. As shown in the image below:
Even when I replace Line 18 of CODE-B with either one of the following statements:
Any of the above lines of code did not seem to do the trick. I could not find the logical reason for that. Anyone of the 3 line of codes (including Line 18 already in CODE-B) should have worked! Probably there is something wrong with the theme I am using. I don´t have a solution for this and I would love your feedback on this on how to exclude Trackbacks/Pingbacks from being counted.
Your Turn to Talk
In this tutorial, I showed you how to display each commenter´s total count without a plugin.
How easy did you find this tutorial to implement? Do you have something to add or anything else to say about displaying the number of comments by author? If so, please share your opinion in the Comment section. Your opinion matters, unless it is a Spam.