最新消息:

WordPress 热评文章

WordPress 热评文章,实际上是指博客中评论最多的文章,而不是访问最多的文章。

而在 WordPress 自身的机制中,每篇文章都已经记录了该文章的评论数目,所以只要通过评论数目排序就能轻松的找出评论最多的文章。

将代码添加到主题的 functions.php 即可:

/*
 功能描述: WordPress 热评文章
 功能介绍页面: https://www.ivicos.com/14.html
*/
function popular_posts($num = 10, $before='<li>', $after='</li>') {
 global $wpdb;
 $sql = "SELECT comment_count,ID,post_title ";
 $sql .= "FROM $wpdb->posts ";
 $sql .= "ORDER BY comment_count DESC ";
 $sql .= "LIMIT 0 , $num";
 $hotposts = $wpdb->get_results($sql);
 $output = '';
 foreach ($hotposts as $hotpost) {
 $post_title = stripslashes($hotpost->post_title);
 $permalink = get_permalink($hotpost->ID);
 $output .= $before.'<a href="' . $permalink . '" rel="bookmark" title="';
 $output .= $post_title . '">' . $post_title . '</a>';
 $output .= $after;
 }
 if($output=='') {
 $output .= $before.'暂无...'.$after;
 }
 echo $output;
}

然后在需要显示热门文章的地方调用如下代码:

<aside class="widget">
 <h3 class="widget-title">热评文章</h3>
 <ul>
 <?php popular_posts(); ?>
 </ul>
</aside>

好了,WordPress 热评文章 就分享到这里,非常感谢你的来访。如果你很喜欢本站,请不要忘记收藏本站,以便下次继续访问;也可以 关注站长微博 随时获取最新动态。你的支持就是我最大的动力!

转载请注明:爱维科斯 » WordPress 热评文章

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章对你有帮助,欢迎点击上方按钮打赏作者