描述
wp_get_archives() 是基于日期的归档列表函数,可以放在等你的WordPress主题模板任意位置。
相关函数:get_archives_link()
用法
默认用法
<?php $args = array( 'type' => 'monthly', 'limit' => '', 'format' => 'html', 'before' => '', 'after' => '', 'show_post_count' => false, 'echo' => 1, 'order' => 'DESC', 'post_type' => 'post' ); wp_get_archives( $args ); ?>
默认情况下,用法显示:
- 显示每月档案链接
- 显示所有档案(不限数量)
- 以降序显示
- HTML列表中的存档每个链接之前或之后都没有显示
- 不显示帖子数量
参数
type
(字符串) 显示列表的类型。默认的WordPress设置。有效值:
■ yearly
■ monthly – 默认值
■ daily
■ weekly
■ postbypost (按照发布时期排序)
■ alpha (例如 postbypost 但是文章按照文章标题排序)
limit
(整数) 获得的文章数量,默认没有限制。
format
◆ (字符串) 文章的列表格式。有效值:
◆ html – 在HTML的列表中
- 标签和前后字符串。这是默认的。
- (string) Format each link should take using the $before and $after args. Accepts ‘link’ (
<link>
tag), ‘option’ (<option>
tag), ‘html’ (<li>
tag), or a custom format, which generates a link anchor with $before preceding and $after succeeding. Default ‘html’.
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) – 默认值post_type
(“string”) 选择文章类型,默认是调用’post’. (since Version 4.4)
示例
<?php $my_archives=wp_get_archives(array( 'type'=>'alpha', 'show_post_count'=>true, 'limit'=>20, 'post_type'=>'post', 'format'=>'html' )); print_r($my_archives); ?>
最近12个月的文章:
<?php wp_get_archives( array( 'type' => 'monthly', 'limit' => 12 ) ); ?>
最近16天的文章:
<?php wp_get_archives( array( 'type' => 'daily', 'limit' => 16) ); ?>
按照文章标题调用最新的20篇文章:
<?php wp_get_archives( array( 'type' => 'postbypost', 'limit' => 20, 'format' => 'custom' ) ); ?>
下拉框的方式,在选择的标签中显示月度档案的下拉框,并显示帖子计数:
<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>
按照字母排序显示所有文章:
<?php wp_get_archives('type=alpha'); ?>