Home > CMS Tutorial > WordPress > body text

How to display top 100 commenters by number of comments

藏色散人
Release: 2019-12-14 14:19:11
forward
1975 people have browsed it

The following WordPress website buildingtutorial column will introduce to you the method of displaying the top 100 commentators according to the number of comments. I hope it will be helpful to friends in need. !

How to display top 100 commenters by number of comments

If you want to see which blogger on your blog has the most comments and the last comment time, the following code will help you realize this function.

You can add the following code to the current theme functions.php:

function top_comment_authors($amount = 100) {
    global $wpdb;
        $prepared_statement = $wpdb->prepare(
        'SELECT
        COUNT(comment_author) AS comments_count, comment_author, comment_author_url, MAX( comment_date ) as last_commented_date
        FROM '.$wpdb->comments.'
        WHERE comment_author != "" AND comment_type = "" AND comment_approved = 1
        GROUP BY comment_author
        ORDER BY comments_count DESC, comment_author ASC
        LIMIT %d',
        $amount);
    $results = $wpdb->get_results($prepared_statement);
    $output = &#39;<ul class="top-comments">&#39;;
    foreach($results as $result) {
        $output .= &#39;<li class="top-comment-author"><strong> <a href="&#39;.$result->comment_author_url.&#39;" target="_blank" rel="external nofollow">&#39;.$result->comment_author.&#39;</a></strong> 共&#39;.$result->comments_count.&#39; 条评论,最后评论 &#39;.human_time_diff(strtotime($result->last_commented_date)).&#39;前</li>&#39;;
    }
    $output .= &#39;</ul>&#39;;
    echo $output;
}
Copy after login

Calling code:

<?php top_comment_authors(100); ?>
Copy after login

Add the code to WordPresstheme template appropriately The position is enough, and the number 100 can control the displayed quantity.

The above is the detailed content of How to display top 100 commenters by number of comments. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:zmingcx.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!