用query_posts列出特定範圍文章 (2)

Time Parameters(時間參數)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
// 小時,範圍從0到23
hour
 
// 分鐘,範圍從0到60
minute
 
// 秒數,範圍從0到60
second
 
// 日,範圍從1到31
day
 
// 月份,範圍從1到12
monthnum
 
// 年份,如2009
year
 
// 一年內的第幾週次,範圍從0到53週
w
?>

範例 1:顯示12月20日發表的文章清單

1
2
3
<?php
query_posts('monthnum=12&day=20');
?>

 

範例 2:顯示本週發表文章清單。

1
2
3
4
5
<?php
$week = date('W');
$year = date('Y');
query_posts('year=' . $year .'&w=' .$week );
?>

 

範例 3:顯示最近30天內發表的文章清單。

1
2
3
4
5
6
7
8
<?php
  function filter_where($where = '') {
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
    return $where;
  }
add_filter('posts_where', 'filter_where');
query_posts($query_string);
?>

 

Pagination Parameters(分頁參數)

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
// 當值設定true時則為不分頁顯示,直接顯示全部文章
nopaging=true
 
// 顯示每頁文章顯示10篇
posts_per_page=10
 
// 頁數,例如當設定為 6 時則就表示跳到第 6 頁
paged=6
 
// 排列順序,ASC為按時間順序排列文章,若是DESC則是反向顯示文章
order=ASC
?>

 

Orderby Parameters(排列順序參數)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
// 依照發表作者排列
orderby=author
 
// 依照日期排列
orderby=date
 
// 依照標題排列
orderby=title
 
// 依照最後編輯時間排列
orderby=modified
 
// 依照分頁順序排列(僅適用於分頁)
orderby=menu_order
 
// (不知道 XD...)
orderby=parent
 
// 依照文章編號排列
orderby=ID
 
// 隨機排列
orderby=rand
 
// 依照自訂欄位數值排列
orderby=meta_value
 
// 依照預設排列
orderby=none
 
// 依照迴響數排列
orderby=comment_count
?>

 

Custom Field Parameters(自訂欄位參數)

1
2
3
4
5
6
7
8
9
10
<?php
// 自訂欄位名稱
meta_key=
 
// 自訂欄位數值
meta_value=
 
// 用來運算meta_value的數值,可以使用的運算符號有 '=', '!=', '>', '>=', '<', 或 '<='
meta_compare=
?>

 

範例 1:顯示自訂欄位名稱為color且它的值為blue的文章。

1
2
3
<?php
query_posts('meta_key=color&meta_value=blue');
?>

 

範例 2:只要自訂欄位數值為blue的文章皆顯示。

1
2
3
<?php
query_posts('meta_value=blue');
?>

範例 3:只要自訂欄位名稱為color的文章皆顯示。

1
2
3
<?php
query_posts('meta_key=color');
?>

範例 4:顯示自訂欄位名稱為color且它的值不為blue的文章。

1
2
3
<?php
query_posts('meta_key=color&meta_compare=!=&meta_value=blue');
?>

 

組合運用範例

範例 1:顯示分類編號為3且是在2004年發表的文章。

1
2
3
<?php
query_posts('cat=3&year=2004');
?>

 

範例 2:顯示分類編號為1及3且每頁顯示兩篇、依照標題逆向排列的文章。

1
2
3
<?php
query_posts(array('category__and'=>array(1,3),'posts_per_page'=>2,'orderby'=>title,'order'=>DESC));
?>

 

範例 3:僅在首頁顯示,並且是在分類編號為13的當月發表文章。

1
2
3
4
5
<?php
if (is_home()) {
	query_posts($query_string . '&cat=13&monthnum=' . date('n',current_time('timestamp')));
}
?>

 

範例 4:顯示分類編號為1且標籤為apples的文章。

1
2
3
<?php
query_posts('cat=1&tag=apples');
?>
目前沒有任何文章。
標籤: