wp_get_document_title()函数

WordPress 开发评论4阅读模式

WordPress 4.4.0 版本开始,加入了wp_get_document_title()函数,而 wp_title();已经不推荐使用。

位置:wp-includes/general-template.php文章源自知更鸟-https://zmingcx.com/wp_get_document_title.html

/**
 * Returns document title for the current page.
 *
 * @since 4.4.0
 *
 * @global int $page  Page number of a single post.
 * @global int $paged Page number of a list of posts.
 *
 * @return string Tag with the document title.
 */
function wp_get_document_title() {

	/**
	 * Filters the document title before it is generated.
	 *
	 * Passing a non-empty value will short-circuit wp_get_document_title(),
	 * returning that value instead.
	 *
	 * @since 4.4.0
	 *
	 * @param string $title The document title. Default empty string.
	 */
	$title = apply_filters( 'pre_get_document_title', '' );
	if ( ! empty( $title ) ) {
		return $title;
	}

	global $page, $paged;

	$title = array(
		'title' => '',
	);

	// If it's a 404 page, use a "Page not found" title.
	if ( is_404() ) {
		$title['title'] = __( 'Page not found' );

		// If it's a search, use a dynamic search results title.
	} elseif ( is_search() ) {
		/* translators: %s: Search query. */
		$title['title'] = sprintf( __( 'Search Results for “%s”' ), get_search_query() );

		// If on the front page, use the site title.
	} elseif ( is_front_page() ) {
		$title['title'] = get_bloginfo( 'name', 'display' );

		// If on a post type archive, use the post type archive title.
	} elseif ( is_post_type_archive() ) {
		$title['title'] = post_type_archive_title( '', false );

		// If on a taxonomy archive, use the term title.
	} elseif ( is_tax() ) {
		$title['title'] = single_term_title( '', false );

		/*
		* If we're on the blog page that is not the homepage
		* or a single post of any post type, use the post title.
		*/
	} elseif ( is_home() || is_singular() ) {
		$title['title'] = single_post_title( '', false );

		// If on a category or tag archive, use the term title.
	} elseif ( is_category() || is_tag() ) {
		$title['title'] = single_term_title( '', false );

		// If on an author archive, use the author's display name.
	} elseif ( is_author() && get_queried_object() ) {
		$author         = get_queried_object();
		$title['title'] = $author->display_name;

		// If it's a date archive, use the date as the title.
	} elseif ( is_year() ) {
		$title['title'] = get_the_date( _x( 'Y', 'yearly archives date format' ) );

	} elseif ( is_month() ) {
		$title['title'] = get_the_date( _x( 'F Y', 'monthly archives date format' ) );

	} elseif ( is_day() ) {
		$title['title'] = get_the_date();
	}

	// Add a page number if necessary.
	if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
		/* translators: %s: Page number. */
		$title['page'] = sprintf( __( 'Page %s' ), max( $paged, $page ) );
	}

	// Append the description or site title to give context.
	if ( is_front_page() ) {
		$title['tagline'] = get_bloginfo( 'description', 'display' );
	} else {
		$title['site'] = get_bloginfo( 'name', 'display' );
	}

	/**
	 * Filters the separator for the document title.
	 *
	 * @since 4.4.0
	 *
	 * @param string $sep Document title separator. Default '-'.
	 */
	$sep = apply_filters( 'document_title_separator', '-' );

	/**
	 * Filters the parts of the document title.
	 *
	 * @since 4.4.0
	 *
	 * @param array $title {
	 *     The document title parts.
	 *
	 *     @type string $title   Title of the viewed page.
	 *     @type string $page    Optional. Page number if paginated.
	 *     @type string $tagline Optional. Site description when on home page.
	 *     @type string $site    Optional. Site title when not on home page.
	 * }
	 */
	$title = apply_filters( 'document_title_parts', $title );

	$title = implode( " $sep ", array_filter( $title ) );

	/**
	 * Filters the document title.
	 *
	 * @since 5.8.0
	 *
	 * @param string $title Document title.
	 */
	$title = apply_filters( 'document_title', $title );

	return $title;
}

如果想要启用 WordPress 主题标题功能,在不安装 WordPress SEO 插件的情况下,可以使用以下代码即可:文章源自知更鸟-https://zmingcx.com/wp_get_document_title.html

function theme_support_title_setup() {
   add_theme_support( 'title-tag' );
}
add_action( 'after_setup_theme', 'theme_support_title_setup' );

反之,如果主题使用了 add_theme_support( 'title-tag' ); 功能,想要取消 WordPress 主题标题功能,可以使用以下代码即可:文章源自知更鸟-https://zmingcx.com/wp_get_document_title.html

remove_action( 'wp_head', '_wp_render_title_tag', 1 );

如果不想取消 WordPress 主题标题功能,又想自定义某些页面 SEO 标题信息,可以使用以下过滤器。文章源自知更鸟-https://zmingcx.com/wp_get_document_title.html

document_title_separator文章源自知更鸟-https://zmingcx.com/wp_get_document_title.html

'document_title_separator' 过滤器来设定标题之间的分隔符。假设想要修改网站名称和标题之间的分隔符为“|”,可以使用以下代码即可:文章源自知更鸟-https://zmingcx.com/wp_get_document_title.html

    function custom_document_title_separator() {
        $separator = ' | ';
        return $separator;
    }
    add_filter('document_title_separator', 'custom_document_title_separator');

document_title_parts文章源自知更鸟-https://zmingcx.com/wp_get_document_title.html

'document_title_parts' 过滤器来设定文档标题的其他组成部分,通过关联数据传递。文章源自知更鸟-https://zmingcx.com/wp_get_document_title.html

例如,首页标题默认是 “网站名称 - 网站描述” 的形式,如果你不想要网站描述,可以删除数组中的 tagline。文章源自知更鸟-https://zmingcx.com/wp_get_document_title.html

function document_title_remove_tagline( $title ){
    if( is_home() && isset( $title['tagline'] ) ) {
        unset( $title['tagline'] );
    }
    return $title;
}
add_filter( 'document_title_parts', 'document_title_remove_tagline' );

假如,你想要改变标题,对于网站分隔符和名称都继续保留,可以使用以下代码:文章源自知更鸟-https://zmingcx.com/wp_get_document_title.html

    function custom_site_seo_title( $title ) {
        $title['title'] = "自定义页面 SEO 标题"
        return $title;
    }
    add_filter('document_title_parts', 'custom_site_seo_title', 10, 1);

pre_get_document_title

'pre_get_document_title' 检查 wp_get_document_title() 是否返回任何东西而不是一个空值。示例代码:

function remove_site_desc_title_home( $title ){
    if ( is_home() || is_front_page() ) {
        $title = get_bloginfo( 'name' );
    }
    return $title;
}
add_filter( 'pre_get_document_title', 'remove_site_desc_title_home' );

'wp_get_document_title();' 相当于 WordPress 4.4.0 之前的 wp_title(); 功能,返回当前页面的文档标题。基础用法如下:

    <head>
        <meta charset="<?php bloginfo( 'charset' ); ?>">
        <meta name="viewport" content="width=device-width" />
        <title><?php echo wp_get_document_title(); ?></title>
        <?php wp_head(); ?>
    </head>

 

 

本站文章大部分始于原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
我的微信
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
知更鸟
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

拖动滑块以完成验证