wp_get_archives函數可以讓是實現年度歸檔、月度歸檔、周歸檔、日歸檔等等,配合 Limit 使用限定顯示數量,甚至可以制作網站地圖!wp_get_archives()可用在模板中的任何位置。
WordPress的widget里提供的文章索引模板,他可以讓你的WordPress博客的側邊欄按月度存檔,方便讀者,方便搜索引擎。但默認顯示的是所有月度歸檔,這對有著兩年,甚至兩年以上歷史的老博客來說,無疑是一種負擔。那長長的一豎排,無表情的豎在那里,顯的呆板缺少靈氣;再者較老的日志也不能適應現在的需要。當一片技術日志讓留言者被挖祖墳般挖了出來,你幾乎都產生了這日志是否是你寫的這種錯覺時,隱藏部分月度歸檔顯得尤為重要了。
用法
<?php wp_get_archives( $args ); ?>
默認設置
<?php $args = array(
'type' => 'monthly',
'limit' => '',
'format' => 'html',
'before' => '',
'after' => '',
'show_post_count' => false,
'echo' => 1,
'order' => 'DESC'
);
wp_get_archives( $args ); ?>
默認情況下,使用顯示:
◆ 每月檔案鏈接顯示◆ 顯示所有檔案(不限數字)◆ 依次在HTML列表顯示檔案◆ 沒有顯示之前或之后的每一個鏈接◆ 不顯示數量的帖子
參數
type
(字符串) 顯示列表的類型。默認的WordPress設置。有效值:■ yearly■ monthly – 默認值■ daily■ weekly■ postbypost (按照發布時期排序)■ alpha (例如 postbypost 但是文章按照文章標題排序)
limit
(整數) 獲得的文章數量,默認沒有限制。
format
◆ (字符串) 文章的列表格式。有效值:◆ html – 在HTML的列表中
<li>
標簽和前后字符串。這是默認的?!?option – 在選項
<select>
標簽或者是下來菜單選項中
<option>
◆ link – 內鏈
<link>
標簽中?!?custom – 自定義列表使用前后字符串。
before
(字符串) 在使用該鏈接時文本到地方 html 或者 custom 格式選項。沒有默認值。
after
(字符串) 在使用該鏈接時文本到地方 html 或者 custom 格式選項。沒有默認值。
show_post_count
(布爾值) 是否在列表中顯示的文章的數目。除了使用所有類型 ‘postbypost’.■ 1 (True)■ 0 (False) – 默認值
echo
(布爾值) 返回值是否直接顯示在網頁中?!?1 (True) – 默認值◆ 0 (False)
order
(string) 如何排列查詢到的文章 (since Version 3.5)■ ASC – 升序排列 (A-Z).■ DESC – 降序排列 (Z-A) – 默認值
wp_get_archives調用實例:
1、以月歸檔方式顯示十二個月的歸檔
按月份顯示存檔列表,只顯示最后十二個月的文章。
<?php wp_get_archives( array( 'type' => 'monthly', 'limit' => 12 ) ); ?>
2、過去十六天
顯示歸檔列表的日期,顯示的最后十六天。
<?php wp_get_archives( array( 'type' => 'daily', 'limit' => 16) ); ?>
3、最后二十個文章
顯示最近二十個最新文章標題所列出的存檔列表。
<?php wp_get_archives( array( 'type' => 'postbypost', 'limit' => 20, 'format' => 'custom' ) ); ?>
4、下拉框
顯示每月檔案的下拉框,在選定的標簽,并統計每月日志數量。
<select name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
<option value=""><?php echo esc_attr( __( 'Select Month' ) ); ?></option>
<?php wp_get_archives( array( 'type' => 'monthly', 'format' => 'option', 'show_post_count' => 1 ) ); ?>
</select>
5、顯示所有文章按字母順序
顯示所有文章按標題方式排列顯示所有日志,特別是如果你想有一個日志檔案,就像一個sitemap。
<?php wp_get_archives('type=alpha'); ?>
源文件
wp_get_archives() is located in wp-includes/general-template.php.