_draft_or_post_title()
云策文档标注
概述
_draft_or_post_title() 函数用于获取文章标题,当标题为空时返回默认字符串“(no title)”。它基于 get_the_title() 实现,并包含 HTML 转义处理。
关键要点
- 函数返回文章标题字符串,若为空则返回翻译后的“(no title)”。
- 参数 $post 可选,可为文章 ID 或 WP_Post 对象,默认使用全局 $post。
- 内部调用 get_the_title() 获取标题,使用 __() 进行本地化,并通过 esc_html() 进行 HTML 转义以确保安全输出。
- 主要用于后台列表表格(如 WP_Posts_List_Table、WP_Media_List_Table)和仪表板小部件中处理标题显示。
代码示例
function _draft_or_post_title( $post = 0 ) {
$title = get_the_title( $post );
if ( empty( $title ) ) {
$title = __( '(no title)' );
}
return esc_html( $title );
}
原文内容
Gets the post title.
Description
The post title is fetched and if it is blank then a default string is returned.
Parameters
Source
function _draft_or_post_title( $post = 0 ) {
$title = get_the_title( $post );
if ( empty( $title ) ) {
$title = __( '(no title)' );
}
return esc_html( $title );
}
Changelog
| Version | Description |
|---|---|
| 2.7.0 | Introduced. |