国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > CMS > Wordpress > 正文

WordPress獲取指定時間內評論最多日志實例

2024-09-07 00:50:15
字體:
來源:轉載
供稿:網友

本文章來給各位同學介紹wordpress所有時間內評論最多日志 本周評論最多日志 本月評論最多日志 最近30天評論最多日志 代碼,有需要了解學習的朋友可參考.

WordPress功能函數query_post()的一種高級用法,就是獲取本周或當月或最近30天評論最多的一定數量的日志.

下面要講的是,通過使用query_posts()函數來獲取本周、本月或最近30天內容評論最多的日志.

WordPress所有時間內評論最多日志

首先,讓我們來看看獲取所有時間內評論最多日志的代碼,代碼如下:

  1. <ul> 
  2. <?php query_posts('post_type=post&posts_per_page=10&orderby=comment_count&order=DESC'); 
  3. while (have_posts()): 
  4. the_post(); ?>  
  5. <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(); ?> 
  6. </ul> 

這段代碼默認顯示前10篇評論最多的日志,數量10可修改為其它數值。

WordPress本周評論最多日志

要顯示本周評論最多日志,我們就可以使用如下的代碼,也就是在前面代碼的基礎上再添加一些額外的參數來實現:

  1. <ul> 
  2. <?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); 
  3. while (have_posts()): 
  4. the_post(); ?> 
  5. <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> 
  6. <?php endwhile; wp_reset_query(); ?> 
  7. </ul> 

WordPress本月評論最多日志

類似地,顯示當月評論最多的日志,可以使用下面的代碼:

  1. <ul> 
  2. <?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(); ?> 
  3. <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> 
  4. <?php endwhile; wp_reset_query(); ?> 
  5. </ul> 

WordPress最近30天評論最多日志

要獲取最近30天內評論最多的日志所用的代碼要復雜一些,代碼如下:

  1. <ul> 
  2. <?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(); ?> 
  3. <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(); ?> 
  4. </ul> 

代碼中的“30 days”可以根據需要修改為其他值(如“1 year”,“7 days”,等),將以上各段代碼放置到需要顯示最熱日志的主題模板文件中適當的位置即可,如邊欄(sidebar.php)等.

query_posts() 確實是一條相當有用的功能函數,就如本文所介紹的一樣,可以為其設定許多參數.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新营市| 昭觉县| 蚌埠市| 泸西县| 项城市| 璧山县| 利川市| 淅川县| 通河县| 兴化市| 大田县| 日照市| 赞皇县| 本溪| 莲花县| 金坛市| 寿光市| 连州市| 河西区| 高要市| 廊坊市| 武冈市| 黎平县| 淮南市| 淮南市| 新和县| 岳普湖县| 屏边| 舒城县| 大邑县| 建瓯市| 彰化市| 海城市| 永新县| 建阳市| 津南区| 盘山县| 磐安县| 墨竹工卡县| 小金县| 姜堰市|