十一年专注,只做WordPress定制开发一件事

日志归档函数wp_get_archives详解及举例

王超
2022-12-17
WP常用函数
776 次

wp_get_archives函数可以让是实现年度归档、月度归档、周归档、日归档等等,配合 Limit 使用限定显示数量,甚至可以制作网站地图!wp_get_archives()可用在模板中的任何位置。

用法

<?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.

文章标签:

WordPress日记主要承接WordPress主题定制开发PSD转WordPressWordPress仿站以及以WordPress为管理后端的小程序、APP,我们一直秉持“做一个项目,交一个朋友”的理念,希望您是我们下一个朋友。如果您有WordPress主题开发需求,可随时联系QQ:919985494 微信:18539976310

搜索

嘿,有问题找我来帮您!