Easily Add WordPress Top Comment Counter Without Plugin

One of the great way to show respect and encourage your blog reader is to show top comment counter in sidebar or in footer section. In comment count section you can list out who is on top of comment list. You can do this by many plugins. But you can also easily add WordPress top comment counter without plugin by adding a simple function in your theme. To add this function your theme widget’s have to accept PHP code. In our previous post we have show you how to allow PHP code in widget area. After allowing PHP in widget area first of all add the following code in your theme’s functions.php file:

add-wordpress-top-commentor-list1

[php]

function top_comment_authors($amount = 10){
global $wpdb;
$results = $wpdb->get_results(‘
SELECT
COUNT(comment_author_email) AS comments_count, comment_author_email, comment_author, comment_author_url
FROM
‘.$wpdb->comments.’
WHERE
comment_author_email != "" AND comment_type = "" AND comment_approved = 1
GROUP BY
comment_author_email
ORDER BY
comments_count DESC, comment_author ASC
LIMIT ‘.$amount
);
$output = "<ul>";
foreach($results as $result){
$output .= "<li>".$result->comment_author."</li>";
}
$output .= "</ul>";
echo $output;
}

[/php]

Here $amount=10 means in list maximum 10 commentors will be shown. If you want more or less then just change the number. After that go to widgets area and take the text widget and place the following code:

[php]

<?php top_comment_authors();?>

[/php]

add-wordpress-top-commentor-list2

Place the text widget wherever you like. In our theme we have placed it in sidebar and here is the output:

add-wordpress-top-commentor-list3

 

 

Author: mytutorialguru

Share This Post On

Submit a Comment

Your email address will not be published. Required fields are marked *