欢迎您光临自学哈网,只为分享网络知识教程,供大家学习参考!

「自学哈网」WordPress 5.5+如何过滤存档页面标题?

作者 : 自学哈 本文共1098个字,预计阅读时间需要3分钟 2022-11-26 共96人阅读
也想出现在这里? 联系我们

WordPress 5.5+如何过滤存档页面标题?我们可以使用自己的HTML标记。

以前,现有的get_the_archive_title挂钩仅限于过滤存档页面上使用的整个标题字符串。一些贡献者指出,此过滤器不够具体,无法满足所有主题开发人员对存档页面标题相关的需求。

此次对 get_the_archive_title() 更改中,将内部$title 变量拆分为 $title 和 $prefix。

通过使用新的 get_the_archive_title_prefix 过滤钩子,前缀现在可以包装在自定义元素中或完全删除。

基本示例

get_the_archive_title_prefix可用于过滤存档页面标题前缀。它仅接受一个参数$prefix,这是一个包含标题的文本前缀的字符串。

要将存档标题前缀替换为另一文本,请使用:

function mytheme_archive_title_prefix( $prefix ){

$prefix = __( ‘Currently viewing archives for:’, ‘my-theme’ );

return $prefix;

}

add_filter( ‘get_the_archive_title_prefix’, ‘mytheme_archive_title_prefix’ );

要完全删除存档标题前缀,请使用:

add_filter( ‘get_the_archive_title_prefix’, ‘__return_empty_string’ );

此更改还将标题部分包装为一个 span 元素,并将原始标题和前缀传递给现有 get_the_archive_title 过滤器,从而允许对存档标题进行进一步的自定义。

该过滤器传递以下参数:

$title:要显示的存档标题

$original_title:无前缀的存档标题

$prefix:存档标题前缀

这是一个使用示例:

function mytheme_get_the_archive_title( $title, $original_title, $prefix ) {

$prefix = ‘<span class=”archive-prefix”>’ . __( ‘Currently viewing archives for:’, ‘my-theme’ ) . ‘</span>’;

$title = ‘<span class=”archive-title”>’ . $original_title . ‘</span>’;

return $prefix . $title;

}

add_filter( ‘get_the_archive_title’, ‘mytheme_get_the_archive_title’, 10, 3 );

本站声明:
本站所有资源来源于网络,分享目的仅供大家学习和交流!如若本站内容侵犯了原著者的合法权益,可联系邮箱976157886@qq.com进行删除。
自学哈专注于免费提供最新的分享知识、网络教程、网络技术的资源分享平台,好资源不私藏,大家一起分享!

自学哈网 » 「自学哈网」WordPress 5.5+如何过滤存档页面标题?
也想出现在这里? 联系我们
© 2022 Theme by - 自学哈网 & WordPress Theme. All rights reserved 浙ICP备2022016594号