1. 首页
  2. WordPress 函数手册

get_the_title()

get_the_title( int|WP_Post $post )

检索文章标题。
Retrieve post title.

目录锚点:#说明#参数#返回#源码#笔记


说明(Description)

如果帖子是受保护的,而访问者不是管理员,则“受保护”将显示在帖子标题之前。如果帖子是私有的,那么“私有”将位于帖子标题之前。


参数(Parameters)

参数 类型 必填 说明
$post (int | WP_Post) 可选 Post ID或WP_Post对象。默认值为全局$post。

返回(Return)

(string)


源码(Source)

/**
 * 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.
 *
 * @since 0.71
 *
 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
 * @return string
 */
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 ) ) {

			/**
			 * Filter 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 ) {

			/**
			 * Filter 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 );
		}
	}

	/**
	 * Filter 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 );
}
更新版本 源码位置 使用 被使用
0.71 wp-includes/post-template.php:117 21 7

笔记(Notes)

get_标题故意允许使用HTML
get_标题应该被转义。
打印当前文章的标题

get_the_title() 为WP2原创文章,链接:https://www.wp2.cn/functions/get_the_title/

发表评论

您的电子邮箱地址不会被公开。