is_post_type_hierarchical()
云策文档标注
概述
is_post_type_hierarchical() 函数用于判断指定的文章类型是否为层级结构(如页面)。如果文章类型不存在,函数也会返回 false。
关键要点
- 函数接受一个必需的字符串参数 $post_type,表示文章类型名称。
- 返回布尔值,指示文章类型是否为层级结构。
- 内部实现先检查文章类型是否存在,然后通过 get_post_type_object() 获取对象并返回其 hierarchical 属性。
- 相关函数包括 post_type_exists() 和 get_post_type_object()。
代码示例
$post_type = 'page';
if ( is_post_type_hierarchical( $post_type ) ) {
echo 'The post type ' . $post_type . ' is hierarchical.';
} else {
echo 'The post type ' . $post_type . ' is not hierarchical.';
}注意事项
- 返回 false 可能表示文章类型不是层级结构,也可能表示文章类型未注册或不存在。
- 函数自 WordPress 3.0.0 版本引入。
- 在多个核心功能中被使用,如构建查询、处理菜单项和重定向旧链接等。
原文内容
Determines whether the post type is hierarchical.
Description
A false return value might also mean that the post type does not exist.
See also
Parameters
$post_typestringrequired-
Post type name
Source
function is_post_type_hierarchical( $post_type ) {
if ( ! post_type_exists( $post_type ) ) {
return false;
}
$post_type = get_post_type_object( $post_type );
return $post_type->hierarchical;
}
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |
Skip to note 2 content
Darshit Rajyaguru
$post_type = 'page'; if ( is_post_type_hierarchical( $post_type ) ) { echo 'The post type <code>' . $post_type . '</code> is hierarchical.'; } else { echo 'The post type <code>' . $post_type . '</code> is not hierarchical.'; }