检索帖子标题。

原型

get_the_title( int|WP_Post $post )

描述

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

参数

$post (int|WP_Post) (Optional)  发布ID或WP_Post对象。默认为全球$ post。

返回值

(string)  

源文件

路径:wp-includes/post-template.php

<?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 );
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/get_the_title/