the_archive_title()
云策文档标注
概述
the_archive_title() 是一个 WordPress 模板函数,用于基于当前查询对象显示归档标题。它通常用于主题文件中,如 archive.php,以输出分类、标签、日期、作者等归档页面的标题。
关键要点
- 函数功能:显示归档标题,基于 get_the_archive_title() 获取标题,并支持通过参数添加前缀和后缀内容。
- 参数说明:接受两个可选参数 $before 和 $after,均为字符串类型,默认值为空,用于在标题前后添加自定义内容。
- 使用场景:适用于各种归档页面,如分类、标签、日期、作者、自定义文章类型等,帮助开发者动态显示页面标题。
- 相关函数:与 get_the_archive_title() 紧密相关,后者用于检索标题而不直接输出;另可参考 the_archive_description() 用于显示描述。
- 版本历史:自 WordPress 4.1.0 版本引入,是核心模板函数的一部分。
代码示例
// 基本用法:显示归档标题,无前后缀
the_archive_title( '', '' );
// 添加自定义前缀和后缀
the_archive_title( '<h1 class="archive-title">', '</h1>' );注意事项
- 函数直接输出标题到页面,若需获取标题字符串而不输出,应使用 get_the_archive_title()。
- 确保在正确的 WordPress 查询上下文中调用,例如在归档模板中,以避免标题显示错误或为空。
- 参数 $before 和 $after 可用于添加 HTML 标签或文本,增强标题的样式和结构。
原文内容
Displays the archive title based on the queried object.
Description
See also
Parameters
$beforestringoptional-
Content to prepend to the title. Default empty.
$afterstringoptional-
Content to append to the title. Default empty.
Source
function the_archive_title( $before = '', $after = '' ) {
$title = get_the_archive_title();
if ( ! empty( $title ) ) {
echo $before . $title . $after;
}
}
Changelog
| Version | Description |
|---|---|
| 4.1.0 | Introduced. |
Skip to note 4 content
rabmalin
Display archive title.
the_archive_title( '<h1 class="page-title">', '</h1>' );Skip to note 5 content
Anthony Hortin
See Also
the_archive_description() – Display category, tag, term, or author description
Skip to note 6 content
Hugo Baeta
Used for returning/displaying the title of the current term, date, post type, post format, or author archive.