本文章來給各位同學介紹wordpress所有時間內評論最多日志 本周評論最多日志 本月評論最多日志 最近30天評論最多日志 代碼,有需要了解學習的朋友可參考.
WordPress功能函數query_post()的一種高級用法,就是獲取本周或當月或最近30天評論最多的一定數量的日志.
下面要講的是,通過使用query_posts()函數來獲取本周、本月或最近30天內容評論最多的日志.
WordPress所有時間內評論最多日志
首先,讓我們來看看獲取所有時間內評論最多日志的代碼,代碼如下:
- <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可修改為其它數值。
WordPress本周評論最多日志
要顯示本周評論最多日志,我們就可以使用如下的代碼,也就是在前面代碼的基礎上再添加一些額外的參數來實現:
- <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>
WordPress本月評論最多日志
類似地,顯示當月評論最多的日志,可以使用下面的代碼:
- <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最近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() 確實是一條相當有用的功能函數,就如本文所介紹的一樣,可以為其設定許多參數.
新聞熱點
疑難解答
圖片精選