WordPress 完成文章评论排行榜

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

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

一切工夫内评论最多日志


<ul> <?php query_posts('post_type=post&posts_per_page=10&orderby=comment_count&order=DESC'); while (have_posts()): the_post(); ?>
<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>
<?php endwhile; wp_reset_query(); ?>
</ul>

这段代码默许显示前10篇评论最多的日志,数量10可修正为其它数值。
本周评论最多日志
要显示本周评论最多日志,咱们就能够应用如下的代码,也就是在后面代码的根底上再增加一些额定的参数来完成:


<ul> <?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(); ?>
<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>
<?php endwhile; wp_reset_query(); ?>
</ul>

最近30天评论最多日志


<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('post_type=post&posts_per_page=10&orderby=comment_count&order=DESC'); while (have_posts()): the_post(); ?>
<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>
<?php endwhile; wp_reset_query(); ?>
</ul>

“30 days”能够依据需求修正为其余值(如“1 year”, “7 days”, 等)。

本月评论最多日志
相似地,显示当月评论最多的日志,能够应用上面的代码:


<ul> <?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(); ?>
<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>
<?php endwhile; wp_reset_query(); ?>
</ul>

欢送补充阐明~

以上就是安达网络工作室关于《WordPress 实现文章评论排行榜》的一些看法。更多内容请查看本栏目更多内容!

本文相关话题: WordPress 评论 排行榜
版权声明:本文为 安达网络工作室 转载文章,如有侵权请联系我们及时删除。
相关文章
WordPress小工具制造办法【左近期评论工具制造】

本文实例讲述了WordPress小工具制造办法。分享给大家供大家参考,详细如下: WordPress是一个领有着无可比拟...

WordPress之给文章内容两头拔出广告的完成办法

不少冤家心愿在文章内容的两头拔出广告(集体以为这个对用户体验有点不太好),上面就来看看如何完成吧。 应...

WordPress中函数get_term_link的参数设置成绩

为何要用 get_term_link?: 新类型的分类是无奈用 <?php echo get_category_link( $category_id ); ?> 输入...

WordPress简略三步完成首页文章显示缩略图(无插件)

喜爱折腾修正Wordpress主题的冤家,一定遇到过这样的成绩,想在首页文章中显示缩略图,有插件能够完成,不过...

WordPress后盾的内容治理性能

要害字形容:性能 治理 内容 后盾 文章 分类 页面 文件 能够 删除 上一次教程咱们解说了应用Wordpress来撰...

WordPress 3.5 与 wpdb::prepare() 报错处理方法

WordPress 3.5 正式公布, 这个版本包括大量更新. 启用了最新的呼应式规划默许主题 Twenty Twelve; 改版了 A...

需求提交

客服服务