帝国CMS技术互助群:540946827 一帮大神带你装逼带你飞!还有漂亮的妹纸陪你玩哦!
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(); ?>
如对本文有疑问,请提交到交流社区,广大热心网友会为你解答!! 点击进入社区
打赏一下,我们会为大家提供更多优质资源!
您的打赏将全部用来搜集更多优质资源!
扫码打赏,你说多少就多少
打开微信扫一扫,即可进行扫码打赏哦