get_the_post_type_description()
云策文档标注
概述
get_the_post_type_description() 函数用于检索文章类型归档页的描述。它从当前查询变量中获取文章类型,并返回其描述字符串,支持通过过滤器进行自定义。
关键要点
- 函数返回文章类型归档的描述字符串,若无描述则返回空字符串。
- 通过 get_query_var() 获取当前查询的文章类型,并处理数组情况。
- 使用 get_post_type_object() 获取文章类型对象,检查其 description 属性。
- 提供 apply_filters('get_the_post_type_description', $description, $post_type_obj) 钩子,允许开发者过滤描述。
- 自 WordPress 4.9.0 版本引入,常用于 get_the_archive_description() 函数中。
代码示例
// 在主题文件中使用示例
$description = get_the_post_type_description();
if ( ! empty( $description ) ) {
echo '<p>' . esc_html( $description ) . '</p>';
}注意事项
- 确保在文章类型归档页上下文中调用,否则可能无法正确获取文章类型。
- 描述内容可通过文章类型注册时的 'description' 参数设置,或通过过滤器动态修改。
原文内容
Retrieves the description for a post type archive.
Source
function get_the_post_type_description() {
$post_type = get_query_var( 'post_type' );
if ( is_array( $post_type ) ) {
$post_type = reset( $post_type );
}
$post_type_obj = get_post_type_object( $post_type );
// Check if a description is set.
if ( isset( $post_type_obj->description ) ) {
$description = $post_type_obj->description;
} else {
$description = '';
}
/**
* Filters the description for a post type archive.
*
* @since 4.9.0
*
* @param string $description The post type description.
* @param WP_Post_Type $post_type_obj The post type object.
*/
return apply_filters( 'get_the_post_type_description', $description, $post_type_obj );
}
Hooks
- apply_filters( ‘get_the_post_type_description’, string $description, WP_Post_Type $post_type_obj )
-
Filters the description for a post type archive.
Changelog
| Version | Description |
|---|---|
| 4.9.0 | Introduced. |