get_the_archive_title
云策文档标注
概述
get_the_archive_title 是一个 WordPress 过滤器钩子,用于修改存档页面的标题显示。它允许开发者自定义标题内容,例如移除前缀或调整格式。
关键要点
- 过滤器钩子:get_the_archive_title,用于过滤存档标题。
- 参数:$title(要显示的存档标题)、$original_title(无前缀的原始标题)、$prefix(存档标题前缀)。
- 版本变化:WordPress 5.5.0 引入了 $prefix 和 $original_title 参数,4.1.0 版本首次引入。
- 相关函数:get_the_archive_title() 用于基于查询对象检索存档标题。
代码示例
// 移除存档标题前缀的示例函数(WordPress 5.5.0 及更高版本)
function wpdocs_remove_archive_title_prefixes( $title, $original_title ) {
return $original_title;
}
add_filter( 'get_the_archive_title', 'wpdocs_remove_archive_title_prefixes', 10, 2 );注意事项
- 在 WordPress 5.5.0 之前,移除前缀可能需要更复杂的条件判断,如用户贡献笔记中的示例所示。
- 使用此钩子时,确保正确处理所有参数以避免意外行为。
原文内容
Filters the archive title.
Parameters
$titlestring-
Archive title to be displayed.
$original_titlestring-
Archive title without prefix.
$prefixstring-
Archive title prefix.
Source
return apply_filters( 'get_the_archive_title', $title, $original_title, $prefix );
Skip to note 3 content
WebMan Design | Oliver Juhas
TIP: Using the hook to get rid of archive “label”:
If you would like to get rid of the “Category:”, “Tag:”, “Author:”, “Archives:” and “Other taxonomy name:” in the archive title, use this little function in your (child) theme
functions.phpfile:function my_theme_archive_title( $title ) { if ( is_category() ) { $title = single_cat_title( '', false ); } elseif ( is_tag() ) { $title = single_tag_title( '', false ); } elseif ( is_author() ) { $title = '<span class="vcard">' . get_the_author() . '</span>'; } elseif ( is_post_type_archive() ) { $title = post_type_archive_title( '', false ); } elseif ( is_tax() ) { $title = single_term_title( '', false ); } return $title; } add_filter( 'get_the_archive_title', 'my_theme_archive_title' );Skip to note 4 content
guardiancrescent
Since 5.5.0 the nicer approach to removing prefixes would be:
function wpdocs_remove_archive_title_prefixes( $title, $original_title ) { return $original_title; } add_filter( 'get_the_archive_title', 'wpdocs_remove_archive_title_prefixes', 10, 2 );