首頁  >  文章  >  CMS教程  >  如何按評論數量顯示前100名評論者

如何按評論數量顯示前100名評論者

藏色散人
藏色散人轉載
2019-12-14 14:19:111936瀏覽

下面由WordPress建站教學專欄給大家介紹按評論數量顯示前100名評論者的方法,希望對需要的朋友有所幫助!

如何按評論數量顯示前100名評論者

如想看看自己部落格上哪位博友的留言留言最多及最後的留言時間,以下一段程式碼會幫你實現這個功能。

可以將下面程式碼新增至目前主題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;
}

呼叫程式碼:

<?php top_comment_authors(100); ?>

將程式碼新增至WordPress主題模板適當位置即可,其中的數字100可以控制顯示數量。

以上是如何按評論數量顯示前100名評論者的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:zmingcx.com。如有侵權,請聯絡admin@php.cn刪除