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言语切换(例如中文版和英文版转换)

首先关上网站根目录下的 wp-config.php,而后搜寻 define('WPLANG' 就能够疾速定位到言语设置那里 比方简体...

WordPress初级自定义规划的内容编辑器(TinyMCE)模板

WordPress的编辑器TinyMCE是一个十分弱小的工具,关于网页设计师来说,应用WordPress的编辑器TinyMCE是没什...

简介WordPress中用于获取首页和站点链接的PHP函数

home_url()(获取首页链接) ome_url() 函数用来获取 WordPress 的首页链接。 用法 home_url( $path, $sch...

wordpress罕用的函数、条件判别以及文件总结

WordPress根本模板文件 一套完好的WordPress模板应至多具备如下文件: style.css : CSS(款式表)文件 index....

WordPress 4.1 公布:开启免干扰写作模式

WordPress 4.1 正式版公布,这次更新带来了2015主题(Twenty Fifteen),免干扰写作模式,暗藏各种芜杂以及...

应用WordPress内置图片仓库制造缩略图的小技巧

WordPress 不只是博客, 很多时分 WordPress 还被用作为 CMS (内容治理零碎). 博主们喜爱为每个文章加上对立...

需求提交

客服服务