查看: 49|回复: 0

[教程] WordPress 实现文章评论排行榜

[复制链接]

6

主题

0

回帖

0

积分

热心网友

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2008-10-15
发表于 2023-10-26 00:00:00 | 显示全部楼层 |阅读模式

用到了WordPress功能函数Query_post()的一种高级用法,就是获取本周或当月或最近30天评论最多的一定数量的日志。

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

所有时间内评论最多日志

  1. <ul> <?php query_posts('postpost_type=post&posts_per_page=10&orderby=comment_count&order=DESC'); while (have_posts()): the_post(); ?> 
  2. <li><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr('Permalink to %s'), the_title_attribute('echo=0')); ?>" rel="bookmark"><?php the_title(); ?></a></li> 
  3. <?php endwhile; wp_reset_query(); ?> 
  4. </ul> 

这段代码默认显示前10篇评论最多的日志,数量10可修改为其它数值。
本周评论最多日志
要显示本周评论最多日志,我们就可以使用如下的代码,也就是在前面代码的基础上再添加一些额外的参数来实现:

  1. <ul> <?php $week = date('W'); $year = date('Y'); query_posts('postpost_type=post&posts_per_page=10&orderby=comment_count&order=DESC&year=' . $year . '&w=' . $week); while (have_posts()): the_post(); ?> 
  2. <li><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr('Permalink to %s'), the_title_attribute('echo=0')); ?>" rel="bookmark"><?php the_title(); ?></a></li> 
  3. <?php endwhile; wp_reset_query(); ?> 
  4. </ul> 

最近30天评论最多日志

  1. <ul> <?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('postpost_type=post&posts_per_page=10&orderby=comment_count&order=DESC'); while (have_posts()): the_post(); ?> 
  2. <li><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr('Permalink to %s'), the_title_attribute('echo=0')); ?>" rel="bookmark"><?php the_title(); ?></a></li> 
  3. <?php endwhile; wp_reset_query(); ?> 
  4. </ul> 

“30 days”可以根据需要修改为其他值(如“1 year”, “7 days”, 等)。

本月评论最多日志
类似地,显示当月评论最多的日志,可以使用下面的代码:

  1. <ul> <?php $month = date('m'); $year = date('Y'); query_posts('postpost_type=post&posts_per_page=10&orderby=comment_count&order=DESC&year=' . $year . '&monthnum=' . $month); while (have_posts()): the_post(); ?> 
  2. <li><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr('Permalink to %s'), the_title_attribute('echo=0')); ?>" rel="bookmark"><?php the_title(); ?></a></li> 
  3. <?php endwhile; wp_reset_query(); ?> 
  4. </ul> 

欢迎补充说明~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部