X

How to Display Comment Count Next to Username in WordPress

Snippets by IsItWP

Do you want to display comment counts for each user next to their username? Showing the comments count of each user can encourage them to post more comments. Let’s take a look at how to display comment count for each user.

Instructions:

1. Add this code to your theme’s functions.php file or in a site-specific plugin:

<?
function commentCount() {
    global $wpdb;
    $count = $wpdb->get_var('SELECT COUNT(comment_ID) FROM ' . $wpdb->comments. ' WHERE comment_author_email = "' . get_comment_author_email() . '"');
    echo $count . ' comments';
}
?>

2. Then, add this snippet to your comments.php file where you want to display the comment count

<?php 
            commentCount(); 
?>

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: Best WordPress Page Builders (Compared).

Comments  Leave a Reply

  1. thanks for this.

  2. Add the first snippet to the functions.php of your wordpress theme, then add the second snippet in the location you want to display the comment count. The place you may want to add it would be beside the users name “comment_author()” you should see that line within your comments.php.

  3. Hi!
    I want to display name of the user who is currently logged in and after logs out his name should be with the comment.
    i’ve used session but when i logout the session destroys an dthe name and the comment disappears because of unsetting the session.
    Thank You!

  4. mlmleadsgenerationblog May 16, 2013 at 10:46 pm

    Hi Kevin, thank you for this tutorial 🙂

    I was wondering…do you know how to add the comment for each user as a sortable column in the Admin panel (the user list)?

    Thank you in advance for your help,
    Roselle

  5. How would you use Transients API with this to cache it? #wordpress

    1. Sent you a link to a great post about this via twitter.

  6. BlackBerry Empire April 12, 2011 at 10:25 pm

    is this only for disqus system?

    1. No this is not just for disqus, should work with any wordpress themes.

  7. BlackBerry Empire April 12, 2011 at 10:24 pm

    I dont see comment author on either comments.php

  8. BlackBerry Empire April 12, 2011 at 10:16 pm

    I am trying to get it in the skeleton theme of wptouch

    1. I have never used that theme but if you add the commentCount() within the comment loop comments should be displayed within a unordered list. It will work, I would have to see the theme file to be sure. Use http://pastebin.com/ to post your code if you wish,

  9. BlackBerry Empire April 12, 2011 at 10:13 pm

    I dont see comment_author

  10. BlackBerry Empire April 12, 2011 at 8:52 pm

    where do we add it?

    1. Add the first snippet to the functions.php of your wordpress theme, then add the second snippet in the location you want to display the comment count. The place you may want to add it would be beside the users name “comment_author()” you should see that line within your comments.php.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!