get_the_title()
云策文档标注
概述
get_the_title() 函数用于检索文章标题,支持处理受保护和私密文章的前缀添加,并可通过过滤器钩子进行自定义。
关键要点
- 函数接受可选参数 $post,可以是文章 ID 或 WP_Post 对象,默认使用全局 $post。
- 返回字符串类型的文章标题,非管理员访问时,受保护文章会添加“Protected:”前缀,私密文章会添加“Private:”前缀。
- 内部使用 get_post() 获取文章数据,并应用 protected_title_format、private_title_format 和 the_title 过滤器。
- 函数允许 HTML 内容,输出时需根据安全需求使用 esc_html() 或 wp_kses_post() 进行转义。
- 相关函数包括 the_title() 用于显示标题,the_title_attribute() 用于属性安全输出。
代码示例
// 打印当前文章标题
echo get_the_title();
// 安全输出标题(转义 HTML)
echo esc_html( get_the_title() );
// 允许特定 HTML 标签输出
echo wp_kses_post( get_the_title() );注意事项
- get_the_title() 返回的标题可能包含通过 the_title 过滤器添加的 HTML 标记,直接输出需谨慎处理安全风险。
- 如需原始标题值(未过滤),可直接访问 WP_Post 对象的 post_title 属性,例如 get_post($post_id)->post_title。
- 在主题或插件开发中,应根据上下文选择适当的转义函数,避免 XSS 漏洞。
原文内容
Retrieves the post title.
Description
If the post is protected and the visitor is not an admin, then “Protected” will be inserted before the post title. If the post is private, then “Private” will be inserted before the post title.
Parameters
Source
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 );
}
Hooks
- apply_filters( ‘private_title_format’, string $prepend, WP_Post $post )
-
Filters the text prepended to the post title of private posts.
- apply_filters( ‘protected_title_format’, string $prepend, WP_Post $post )
-
Filters the text prepended to the post title for protected posts.
- apply_filters( ‘the_title’, string $post_title, int $post_id )
-
Filters the post title.
Changelog
| Version | Description |
|---|---|
| 0.71 | Introduced. |
Skip to note 5 content
Jon (Kenshino)
get_the_title intentionally allows for HTML
So get_the_title should not be escaped.
Use
the_title_attribute()instead ofget_the_title()if you’re outputting the post title for html attributes.<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"></a>Skip to note 6 content
Tom J Nowell
get_the_titleshould 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:
scripttag with malicious javscriptAll 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() );get_the_title(), don’t be surprised when you get escaped tags in your output. If you want just the title with without any markup, you need to usethe_title_attribute()instead, as described by @kenshino above. Plugins can and do add markup to the the title via the hookthe_title.Skip to note 7 content
znowebdev
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( get_the_title() ) . '">' . get_the_title() . '</a>'; echo '</div>';Skip to note 8 content
erdembircan
get_the_titleis being filtered before value return. If you are checking for raw value of a post title, for empty titles you might get an ‘untitled’ string value depending on the locale of the blog. In order to get raw value of a post title, useget_postand access itspost_titleproperty