用到了WordPress功能函數(shù)Query_post()的一種高級用法,就是獲取本周或當月或最近30天評論最多的一定數(shù)量的日志。
使用方法是將以下各段代碼放置到需要顯示最熱日志的主題模板文件中適當?shù)奈恢眉纯桑邕厵冢╯idebar.php)。
所有時間內(nèi)評論最多日志
- <ul> <?php query_posts('postpost_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篇評論最多的日志,數(shù)量10可修改為其它數(shù)值。
本周評論最多日志
要顯示本周評論最多日志,我們就可以使用如下的代碼,也就是在前面代碼的基礎上再添加一些額外的參數(shù)來實現(xiàn):
- <ul> <?php $week = date('W'); $year = date('Y'); query_posts('postpost_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('postpost_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”可以根據(jù)需要修改為其他值(如“1 year”, “7 days”, 等)。
本月評論最多日志
類似地,顯示當月評論最多的日志,可以使用下面的代碼:
- <ul> <?php $month = date('m'); $year = date('Y'); query_posts('postpost_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>
歡迎補充說明~