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