返回介绍

wp_get_document_title()

发布于 2017-09-11 12:04:09 字数 6224 浏览 1202 评论 0 收藏 0

wp_get_document_title()

Returns document title for the current page.


description


返回值

(string) Tag with the document title.


源代码

File: wp-includes/general-template.php

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 phrase */
		$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() && $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() ) {
		$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 ) );
	$title = wptexturize( $title );
	$title = convert_chars( $title );
	$title = esc_html( $title );
	$title = capital_P_dangit( $title );

	return $title;
}

更新日志

Versiondescription
4.4.0Introduced.

相关函数

Uses

  • wp-includes/general-template.php: pre_get_document_title
  • wp-includes/general-template.php: document_title_separator
  • wp-includes/general-template.php: document_title_parts
  • wp-includes/l10n.php: __()
  • wp-includes/l10n.php: _x()
  • wp-includes/formatting.php: capital_P_dangit()
  • wp-includes/formatting.php: esc_html()
  • wp-includes/formatting.php: convert_chars()
  • wp-includes/formatting.php: wptexturize()
  • wp-includes/general-template.php: get_search_query()
  • wp-includes/general-template.php: get_the_date()
  • wp-includes/general-template.php: get_bloginfo()
  • wp-includes/general-template.php: post_type_archive_title()
  • wp-includes/general-template.php: single_term_title()
  • wp-includes/general-template.php: single_post_title()
  • wp-includes/query.php: is_404()
  • wp-includes/query.php: is_search()
  • wp-includes/query.php: is_singular()
  • wp-includes/query.php: is_year()
  • wp-includes/query.php: is_front_page()
  • wp-includes/query.php: is_tax()
  • wp-includes/query.php: is_home()
  • wp-includes/query.php: is_category()
  • wp-includes/query.php: is_tag()
  • wp-includes/query.php: is_author()
  • wp-includes/query.php: is_month()
  • wp-includes/query.php: is_day()
  • wp-includes/query.php: is_post_type_archive()
  • wp-includes/query.php: get_queried_object()
  • wp-includes/plugin.php: apply_filters()
  • Show 25 more uses Hide more uses

Used By

  • wp-includes/feed.php: get_wp_title_rss()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Khoi Nguyen

    To use: add to header.php

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文