get_the_title

函数
get_the_title ( $post = 0 )
参数
  • (int|WP_Post) $post Optional. Post ID or WP_Post object. Default is global $post.
    Required:
返回值
  • (string)
定义位置
相关方法
get_the_timeget_the_title_rssthe_titleget_the_dateget_the_id
引入
0.71
弃用
-

get_the_title:这是一个WordPress的函数,用于返回循环中的当前文章的标题: 这个函数可以在循环中或在循环之外使用: 这个函数返回的标题是经过格式化的,可以安全显示。

检索文章的标题。

如果文章是受保护的,并且访问者不是管理员,那么””受保护的””将被插入到文章标题前。如果文章是私有的,那么””私有””将被插入到文章标题前。

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

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

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

			/* translators: %s: Protected post title. */
			$prepend = __( 'Protected: %s' );

			/**
			 * 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', $prepend, $post );

			$post_title = sprintf( $protected_title_format, $post_title );
		} elseif ( isset( $post->post_status ) && 'private' === $post->post_status ) {

			/* translators: %s: Private post title. */
			$prepend = __( 'Private: %s' );

			/**
			 * 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', $prepend, $post );

			$post_title = sprintf( $private_title_format, $post_title );
		}
	}

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

常见问题

FAQs
查看更多 >