How to display author’s comment count in WordPress

In this tutorial, I will show you how to display the count of the total number of comments posted by the author in the comments section of an article without the need for a plugin.

As shown in the above figure, whenever someone comments on any article, the total number of comments of the author will be displayed next to his/her name. This is a very useful feature. Friends who like toss can try.

The following is the function of the code:

  • Only approved comments are counted. Pending (and spam) comments are not counted.
  • The test code works fine on WordPress version 3.8.0, but it will work on earlier versions.
  • The code is based on the author’s comment on their email address. Even unregistered user comments are counted.

Here is how to display the number of author comments.

Step 1: Count the author’s comment core code and put it in functions.php

Open the functions.php file located in the theme folder and add (copy and paste) the following code. Save the file and upload it to the server.

<?php
    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)';
        }
   }
?>

Step 2: Display the comment count

After adding the code from step 1 to the theme’s functions.php, you now need to display the comment count. To do this, there are three situations. It all depends on the theme you use:
open the comments.php file for your theme. If in this file you find…

1. The wp_list_comments() function with callback parameters. It’s just like:

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

Then, in this case, you need to edit the mytheme_comment function located in the functions.php file of the theme.

2. The wp_list_comments() function without callback. It’s just like:

<?php wp_list_comments(); ?>

Then, in this case, use the default WordPress wp_list_comments function. 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, please feel free to ask. I will show you how to make a donation to this blog. Your donation helps keep the new code snippet coming soon.

3. If your theme does not have the above two situations. Please refer to the following example to set

<?php
    function mytheme_comment($comment, $args, $depth) 
    {    
        $GLOBALS['comment'] = $comment;
?>
<li <?php comment_class('clearfix') ; ?> id = "li-comment-<?php comment_ID() ?>">
    <?php echo get_avatar( $comment,$size = '38' ); ?> 
<div id="comment-<?php comment_ID(); ?>">
    <div class = "comment-meta commentmetadata">
        <?php printf ( __ ( '<strong>%s</strong>' ) , get_comment_author_link()) ?>
        <?php edit_comment_link ( __ ( '(Edit)' ) , '' , '' ) ?> 
        <span>
            <?php printf ( __ ( '%1$s at %2$s' ) , get_comment_date() , get_comment_time()) ?> 
            <?php bac_comment_count_per_user() ; //Display the total Comment count per Author ?>
        </span>
    </div>
<div class = "text">
    <?php comment_text() ?>
</div>
<?php if ($comment->comment_approved == '0'): ?> 
<em><?php _e('Your comment is awaiting moderation.') ?></em>
<?php endif ; ?>
<div class = "reply">
<?php 
        comment_reply_link (
            array_merge ( $args, array (  
                'depth' = > $depth, 
                'max_depth' = > $args['max_depth'] 
                )
            )
        ) ?>
</div>
<?php } ?>

carry out. If you want to customize your CSS, please modify it yourself.

admin

Related Posts
Leave a reply
Captcha Click on image to update the captcha .