wordpress函数wp_get_archives()用法

该标签显示一个基于日期的存档列表。参数变量以查询字符串形式传递给该标签。wp_get_archives()可用在模板中的任何位置。

<?php wp_get_archives(‘type=monthly&format=html&show_post_count=true’); ?>

意思是按月归档,以<li>HTML列表形式,显示文章数量.
默认设置:

<?php $args = array(
     ’type’            => ’monthly’,
     ’limit’           => ,
     ‘format’          => ‘html’, 
     ‘before’          => ,
     ’after’           => ,
     ‘show_post_count’ => false,
     ‘echo’            => 1 ); 
?>

默认情况下的显示状态:

  • 按月显示存档链接
  • 显示所有存档文章(没有数量限制)
  • 以<li>HTML列表形式显示存档
  • 存档链接前后不显示任何内容
  • 不显示存档下的文章总数

参数详细说明:

type

(字符串)将要显示的存档列表的类型。默认遵循WordPress的设置。有效值包括:

  • yearly
  • monthly ——默认值
  • daily
  • weekly
  • postbypost (以发表日期为顺序排列日志)
  • alpha (与postbypost相同,但以日志标题为顺序排列日志)

limit

(整数)可获取的存档数量。默认为无限制。

format

(字符串)存档列表的格式。有效值包括:

  • html —— 在HTML 列表(<li>)标签与beforeafter字符串内。默认值。
  • option —— 在选择符(<select>)或下拉式选项(<option>)标签中。
  • link —— 在链接(<link>)标签中
  • custom list—— 使用beforeafter字符串的自定义列表

before

(字符串)当format值为htmlcustom时,放在链接前的文字。无默认值。

after

(字符串)当format值为htmlcustom时,放在链接后的文字。无默认值。

show_post_count

(字符串)是否显示存档中的文章总数。用于’postbypost’外所有类型。

  • 1 (true)
  • 0(false)——默认值

echo

(布尔型)显示结果或返回。

  • 1 (true)——默认值
  • 0(false)

使用实例

<?php
//按月输出最近12个月的文章归档链接
 wp_get_archives('type=monthly&limit=12'); 
//按天输出,最近15天的文章归档链接
wp_get_archives('type=daily&limit=15');
//按日期输出每一篇文章的链接,可以用作网站地图。
wp_get_archives('type=postbypost');
?>
//在 select 标签中的演示
//这个例子很有实用价值。
<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( 'type=monthly&format=option&show_post_count=1' ); ?>
</select>