WordPress功能函数Query_post()的一种高级用法,就是获取本周或当月或最近30天评论最多的一定数量的日志,下面要讲的是,通过使用query_posts()函数来获取本周、本月或最近30天内容评论最多的日志。
  首先,让我们来看看获取所有时间内评论最多日志的代码:
<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>
  类似地,显示当月评论最多的日志,可以使用下面的代码:
<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>
  最近30天评论最多日志
  要获取最近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”, 等)。将以上各段代码放置到需要显示最热日志的主题模板文件中适当的位置即可,如边栏(sidebar.php)等。query_posts() 确实是一条相当有用的功能函数,就如本文所介绍的一样,可以为其设定许多参数。

1.《wordpress博客获取本周或本月评论最多日志》援引自互联网,旨在传递更多网络信息知识,仅代表作者本人观点,与本网站无关,侵删请联系页脚下方联系方式。

2.《wordpress博客获取本周或本月评论最多日志》仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证。

3.文章转载时请保留本站内容来源地址,https://www.cxvn.com/study/35.html