搜索
SR網頁設計工作室 - 購物網站/企業官網專業開發 Wordpress 使用教程 使用 Query_post() 獲取Wordpress本週或本月評論最多文 ...
byadmin 發表於 2013-5-21 20:07:37 , 3895人已閱讀 , 0人回應
本文要談論WordPress功能函數Query_post()的一種高級用法,就是將它用來實現獲取本週、當月或最近30天評論最多一定數量的文章。

下面我們來藉由使用query_posts()函數來獲取本週、本月或最近30天內容評論最多的文章。


所有時間內評論最多文章

首先,讓我們來看看獲取所有時間內評論最多文章的代碼:
  1. <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可自定義其他數值。


本週評論最多文章

要顯示本週評論最多文章,我們就可以使用如下的代碼,也就是在前面代碼的基礎上再添加一些額外的參數來實現:
  1. <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>
複製代碼

本月評論最多文章

顯示當月評論最多的文章:
  1. <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天內評論最多的文章:
  1. <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() 可以讓我們簡單實現各個文章的統計需求 ~  

收藏回覆 只看該作者 道具 舉報

Traffic Exchange Site
您需要登錄後才可以回帖 登錄 | 立即註冊

作者資訊

文章分類

SR數位設計工作室

  • 服務專線:03-3555-069


ECSHOP購物網站開發|接案說明|線上洽詢|隱私權政策|SR數位設計(CMS)

信箱:sr.design2011@gmail.com

, Processed in 0.176766 second(s), 44 queries , Gzip On.

Copyright © 2011-2014 Template By SR網頁設計清新部落風格

Core - DZ2.5 GMT+8, 2024-4-20 20:03

回頂部