php按文章评论数排序,WordPress文章按评论进行排序的实现方法

c58265e72272c8461c5092b8cc4f6095.gif

帝国CMS技术互助群:540946827 一帮大神带你装逼带你飞!还有漂亮的妹纸陪你玩哦!

a0f609b288c7de88131b6f6ba37c81b3.png

WordPress默认没有按文章评论进行排序的功能,如果我们需要这个功能就需要用到WordPress功能函数Query_post()的一种高级用法,就是获取本周或当月或最近30天评论最多的一定数量的日志。

使用方法是将以下各段代码放置到需要显示最热日志的主题模板文件中适当的位置即可,如边栏(sidebar.php)。

所有时间内按评论量排序:

  • <?php query_posts('post_type=post&posts_per_page=10&orderby=comment_count&order=DESC'); while (have_posts()): the_post(); ?>
  • <?php the_title(); ?><?php endwhile; wp_reset_query(); ?>

这段代码默认显示前10篇评论最多的日志,数量10可修改为其它数值。

一周内的评论最多文章:

  • <?php $week = date('W'); $year = date('Y'); query_posts('post_type=post&posts_per_page=10&orderby=comment_count&order=DESC&year=' . $year . '&w=' . $week); while (have_posts()): the_post(); ?>
  • <?php the_title(); ?><?php endwhile; wp_reset_query(); ?>

最近30天评论最多日志:

  • <?php function filter_where($where = '') { //posts in the last 30 days $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'"; return $where; } add_filter('posts_where', 'filter_where'); query_posts('post_type=post&posts_per_page=10&orderby=comment_count&order=DESC'); while (have_posts()): the_post(); ?>
  • <?php the_title(); ?><?php endwhile; wp_reset_query(); ?>

本月评论最多日志:

  • <?php $month = date('m'); $year = date('Y'); query_posts('post_type=post&posts_per_page=10&orderby=comment_count&order=DESC&year=' . $year . '&monthnum=' . $month); while (have_posts()): the_post(); ?>
  • <?php the_title(); ?><?php endwhile; wp_reset_query(); ?>

如对本文有疑问,请提交到交流社区,广大热心网友会为你解答!! 点击进入社区

打赏一下,我们会为大家提供更多优质资源!

91e7dcaaa1a0bd41a6bdf0bdc709f045.png

您的打赏将全部用来搜集更多优质资源!

weipayimg.jpg

扫码打赏,你说多少就多少

cc16f7477eabc8420c6a2b3eb63a44b8.png

cbb949506e1f1b1153ebda7bc4c213c4.png

打开微信扫一扫,即可进行扫码打赏哦

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值