WordPress函数:wp_get_archives根据日期显示日志归档详解举例

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

    ◆ (字符串) 文章的列表格式。有效值:
    1. ◆ html – 在HTML的列表中

<li>

    标签和前后字符串。这是默认的。
    1. ◆ option – 在选项

<select>

    1. 标签或者是下来菜单选项中

<option>

    1. ◆ 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.

评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注