当前浏览:首页 / WordPress钩子 / pre_get_document_title

apply_filters( 'pre_get_document_title', string $title )

在生成文档标题之前过滤文档标题

document

premore...

titlemore...


描述

传递非空值将使wp_get_document_title()短路,而返回该值。


参数

$titlestring
文档标题。默认空字符串。


源码

查看源码 官方文档


更新日志

版本描述
4.4.0开始引入

使用示例

  • 示例1

    如果要从主页中删除“说明”:

    function wporg_only_title_home_page( $title )
    {
    	if ( is_home() || is_front_page() ) {
    		$title = get_bloginfo( 'name' );
    	}
    
    	return $title;
    }
    add_filter( 'pre_get_document_title', 'wporg_only_title_home_page' );
  • 示例2

    如果要更改自定义文章类型的存档页面标题,请使用以下代码:

    function change_custom_post_type_archive_title( $title ) {
        if ( is_post_type_archive( 'your-custom-post-type' ) ){
            $title = 'My Custom post type archive - ' . get_bloginfo('name');
            return $title;
        }
        return $title;
    }
    
    add_filter( 'pre_get_document_title', 'change_custom_post_type_archive_title', 9999 );