新闻中心

wordpress实现评论最多的前50名显示评论的方法

作者 / 无忧主机 时间 2018-10-15 00:32:59

有很多站长朋友使用的是wordpress在PHP虚拟主机中做的一个技术博客或者是做的是其他的方面的,比如很多说是评论有奖呀,或者是其他的原因,那么很多的站长朋友希望把此篇文章评论的前五十名进行显示出来,如何操作呢?接下来就跟着无忧主机小编的步伐一步步的操作即可实现。 1、首先进入到我们v3.51php.com,然后登录进去,点击主机管理,点击控制面板,如图1所示: 1 2、然后点进去找到,找到文件管理,如图2所示: 2 3、再然后进入到了文件管理,找到根目录,如图3所示: 3 4、然后找到wordpress的主题目录,/wp-content/themes/您的主题/functions.php,如图4所示: 4 5、点击编辑,把如下代码加入到?>的前面,如图5所示: 5

function top_comment_authors($amount = 50) {
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 = '<ul class="top-comments">'; foreach($results as $result) { $output .= '<li class="top-comment-author"><strong> <a href="'.$result->comment_author_url.'" target="_blank" rel="external nofollow">'.$result->comment_author.'</a></strong> 共'.$result->comments_count.' 条评论,最后评论 '.human_time_diff(strtotime($result->last_commented_date)).'前</li>'; } $output .= '</ul>'; echo $output; } 加入之后点击保存,在您的主题适当的位置进行调用即可,调用代码如下: <?php top_comment_authors(50); ?> 就这样我们就实现了这个简单的功能。 以上是无忧主机独立IP虚拟主机给您带来的介绍。

本文地址:https://www.51php.com/wordpress/27780.html

1
1
1
1
1
1
1

客户服务热线

0791-8623-3537

在线客服