get_the_title() - WordPress 函数参考中文文档

返回介绍

get_the_title()

发布于 2017-09-11 字数7440 浏览 853 评论 0

get_the_title( int|WP_Post $post )

Retrieve post title.


描述

If the post is protected and the visitor is not an admin, then "Protected" will be displayed before the post title. If the post is private, then "Private" will be located before the post title.


参数

$post

(int|WP_Post)
(Optional)
Post ID or WP_Post object. Default is global $post.


返回值

(string)


源代码

File: wp-includes/post-template.php

function get_the_title( $post = 0 ) {
	$post = get_post( $post );

	$title = isset( $post->post_title ) ? $post->post_title : '';
	$id = isset( $post->ID ) ? $post->ID : 0;

	if ( ! is_admin() ) {
		if ( ! empty( $post->post_password ) ) {

			/**
			 * Filters the text prepended to the post title for protected posts.
			 *
			 * The filter is only applied on the front end.
			 *
			 * @since 2.8.0
			 *
			 * @param string  $prepend Text displayed before the post title.
			 *                         Default 'Protected: %s'.
			 * @param WP_Post $post    Current post object.
			 */
			$protected_title_format = apply_filters( 'protected_title_format', __( 'Protected: %s' ), $post );
			$title = sprintf( $protected_title_format, $title );
		} elseif ( isset( $post->post_status ) && 'private' == $post->post_status ) {

			/**
			 * Filters the text prepended to the post title of private posts.
			 *
			 * The filter is only applied on the front end.
			 *
			 * @since 2.8.0
			 *
			 * @param string  $prepend Text displayed before the post title.
			 *                         Default 'Private: %s'.
			 * @param WP_Post $post    Current post object.
			 */
			$private_title_format = apply_filters( 'private_title_format', __( 'Private: %s' ), $post );
			$title = sprintf( $private_title_format, $title );
		}
	}

	/**
	 * Filters the post title.
	 *
	 * @since 0.71
	 *
	 * @param string $title The post title.
	 * @param int    $id    The post ID.
	 */
	return apply_filters( 'the_title', $title, $id );
}

更新日志

Version 描述
0.71 Introduced.

相关函数

Uses

  • wp-includes/l10n.php:
    __()
  • wp-includes/load.php:
    is_admin()
  • wp-includes/plugin.php:
    apply_filters()
  • wp-includes/post-template.php:
    protected_title_format
  • wp-includes/post-template.php:
    private_title_format
  • wp-includes/post-template.php:
    the_title
  • wp-includes/post.php:
    get_post()
  • Show 2 more uses
    Hide more uses


Used By

  • wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php:
    WP_REST_Revisions_Controller::prepare_item_for_response()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:
    WP_REST_Posts_Controller::prepare_item_for_response()
  • wp-includes/embed.php:
    wp_embed_excerpt_more()
  • wp-includes/embed.php:
    get_post_embed_html()
  • wp-admin/includes/template.php:
    _draft_or_post_title()
  • wp-admin/includes/revision.php:
    wp_prepare_revisions_for_js()
  • wp-admin/includes/class-wp-comments-list-table.php:
    WP_Comments_List_Table::column_response()
  • wp-admin/includes/class-walker-nav-menu-edit.php:
    Walker_Nav_Menu_Edit::start_el()
  • wp-admin/includes/nav-menu.php:
    _wp_ajax_menu_quick_search()
  • wp-includes/widgets/class-wp-widget-recent-posts.php:
    WP_Widget_Recent_Posts::widget()
  • wp-includes/widgets/class-wp-widget-recent-comments.php:
    WP_Widget_Recent_Comments::widget()
  • wp-includes/feed.php:
    get_the_title_rss()
  • wp-includes/post-template.php:
    the_title()
  • wp-includes/post-template.php:
    the_title_attribute()
  • wp-includes/class-wp-xmlrpc-server.php:
    wp_xmlrpc_server::_prepare_comment()
  • wp-includes/comment-template.php:
    trackback_rdf()
  • wp-includes/comment-template.php:
    comments_popup_link()
  • wp-includes/class-wp-editor.php:
    _WP_Editors::wp_link_query()
  • Show 13 more used by
    Hide more used by


User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 4You must log in to vote on the helpfulness of this note

    Contributed by Jon (Kenshino)

    get_the_title intentionally allows for HTML

    So get_the_title should not be escaped.

    Use the_title_attribute() instead of get_the_title() if you’re outputting the post title for html attributes.

    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
  2. get_the_title should be escaped.

    Super admins and administrators have the ability to enter arbitrary HTML in the title field, but that doesn’t prevent problems from appearing, for example:

    • A rogue administrator adds a script tag with malicious javscript
    • A hacker manages to change the title via an exploit
    • A compromised plugin uses a filter to change the title
    • A broken plugin allows it to be changed
    • A hacker has broken into Redis/APC/Memcached and modified the cache
    • File based caches have been compromised

    All of this is a non-issue with escaping, which makes sure what’s outputted is what you expected. That doesn’t mean you can’t let users put HTML in there, as long as you specify which tags are allowed

    To display the title safely, do this:

    echo esc_html( get_the_title() );

    And if you want the title to include HTML tags:

    echo wp_kses_post( get_the_title() );

    Print the current post’s title

    
    echo get_the_title();
    

    Simple breadcrumb trail for pages, two levels deep.

     
    echo '<div class="breadcrumb">';
    	// If there is a parent, display the link.
    	$parent_title = get_the_title( $post->post_parent );
    
    	if ( $parent_title != the_title( ' ', ' ', false ) ) {
    		echo '<a href="' . esc_url( get_permalink( $post->post_parent ) ) . '" alt="' . esc_attr( $parent_title ) . '">' . $parent_title . '</a> » ';
    	}
    
    	// Then go on to the current page link.
    	echo '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark" alt="' . esc_attr( the_title_attribute() ) . '">' . the_title() . '</a>';
    echo '</div>';
    

上一篇:get_the_time()

下一篇:get_the_title_rss()

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!