WordPress归档列表标签:wp_get_archives

WordPress模板标签wp_get_archives用于显示一个基于日期的归档列表,这个标签可以用在任何模板,通常用来制作一个日期归档页。

wp_get_archives( array|string $args = '' )

函数参数

$args

数组或字符串值

wp_get_archives()函数$args参数默认的值如下:

$args = array(
	'type'				=> 'monthly',
	'limit'				=> '',
	'format'			=> 'html', 
	'before'			=> '',
	'after'				=> '',
	'show_post_count'	=> false,
	'echo'				=> 1,
	'order'				=> 'DESC',
	'post_type'			=> 'post'
);

wp_get_archives()函数$args参数可用的值如下:

type

字符串值,默认值:monthly

归档的方式,以年、月、日、周等归档

  • yearly:按年份归档
  • monthly:按月份归档
  • daily:按天归档
  • weekly:按周归档
  • postbypost:按发表日期列出所有文章
  • alpha:按文章标题排序列出所有文章

limit

整数型,默认为空

显示的数量,默认为无限

format

字符串值,默认值:html

以何种HTML标签输出内容,默认以<li>列表输出。

  • html:以<li>列表输出;
  • option:以<option>输出,需要自己添加<select>标签;
  • link:以<link>标签输出,rel值为archives;
  • custom:自定义包裹链接的标签,将使用before和after定义的值;

before

字符串值,默认为空

当format的值为custom时,before的值将在链接前输出,即包裹链接的开始标签。

after

字符串值,默认为空

当format的值为custom时,after的值将在链接后输出,即包裹链接的开始标签。

show_post_count

布尔值,默认值:false

显示文章数量,type的值不为postbypost或alpha时。

echo

布尔值,默认值:1

是否输出结果,如果为0则只返回结果而不是输出。

order

字符串值,默认值:ASC

排序方式

  • ASC:升序
  • DESC:降序

post_type

字符串值,默认值:post

文章类型

函数使用示例

按年份输出归档,并显示文章数量

<?php
	$args = array(
		'type' => 'yearly',
		'show_post_count' => true
	);
	wp_get_archives( $args );
?>

下面的代码输出一样的结果:

<?php wp_get_archives( 'type=yearly&show_post_count=1' ); ?>

扩展阅读

wp_get_archives()函数位于:wp-includes/general-template.php

阿里云