新闻中心

WordPress网站程序如何添加文章排行榜

作者 / 无忧主机 时间 2014-08-05 10:25:14

最近wordpress的问题层出不穷,一个谷歌退出了中国市场就造成了wordpress的卡顿,无忧主机小编最近一个月安装wordpress的插件都不下100个了。所以 现在小编对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> 希望可以帮助到各位站长朋友!

本文地址:https://www.51php.com/wordpress/15562.html

1
1
1
1
1
1
1

客户服务热线

0791-8623-3537

在线客服