WordPress 实现文章评论排行榜

所属分类: CMS教程 / WordPress 阅读数: 591
收藏 0 赞 0 分享

用到了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 防止恶意评论代码处理办法
收藏 0 赞 0 分享

wordpress制作自定义菜单的方法

本文介绍了wordpress中自定义菜单制作详细教程
收藏 0 赞 0 分享

WordPress增加文章排序方式

很多网站的文章列表页面都可以查看排序方法,但是在wp系统中是没有这个功能,下面我们来看看关于WordPress系统中增加选择文章的排序方式的解决办法
收藏 0 赞 0 分享

wordpress get_posts函数的使用方法 禁止输出指定类别的文章

本文向大家介绍wordpress使用get_posts函数功能禁止输出指定类别文章的方法,大家可以参考一下
收藏 0 赞 0 分享

在wordpress文章末尾添加内容的简单方法

如何在wordpress的文章末尾添加内容,在wordpress每篇文章的末尾添加文字、链接等内容,比如你想加个“原创文章如转载,请注明本文链接:”,其实很简单,有多种方法,下面就介绍给大家
收藏 0 赞 0 分享

wordpress dynamic_sidebar()函数使用方法

本文简单介绍wordpress dynamic_sidebar()函数使用的方法,在wordpress开发中会经常用到
收藏 0 赞 0 分享

WordPress导航菜单函数wp_nav_menu()详解

本文主讲内容是WordPress导航菜单函数wp_nav_menu()详细使用的说明,大家可以看参考一下,在开发WordPress模板时会使用到这个函数自定义菜单
收藏 0 赞 0 分享

WordPress自定义多个边栏的方法

在wordpress后台中,在外观>小工具里面,可以自定义边栏要显示的内容,一般的主题都支持至少一个自定义边栏,那么它是如何实现的呢
收藏 0 赞 0 分享

wordpress全局变量$wpdb初始化并声明为全局变量的方法

wordpress操作数据库用一个全局变量$wpdb来进行各种操作,使用的时候先在函数声明global $wpdb,然后调用它的数据库操作方法。那么它是在哪里进行初始化并声明为全局变量的呢
收藏 0 赞 0 分享

wordpress中强大的调用文章函数query posts 用法

query posts是一个非常好用的调用文章函数,可以做到同页面内显示多种特定范围的文章,下面为大家详细的介绍下wordpress中强大的query posts 用法,喜欢的朋友可以参考下
收藏 0 赞 0 分享
查看更多