函数文档

is_taxonomy_hierarchical()

💡 云策文档标注

概述

is_taxonomy_hierarchical() 函数用于判断指定的分类法对象是否为层级结构。它首先验证分类法是否存在,然后获取其对象并返回 hierarchical 属性值。

关键要点

  • 函数接受一个必需参数 $taxonomy,表示分类法名称,返回布尔值指示是否为层级分类法。
  • 如果分类法不存在,函数返回 false,这可能意味着分类法无效或未注册。
  • 函数内部依赖 taxonomy_exists() 和 get_taxonomy() 来确保操作安全性和准确性。
  • 该函数在 WordPress 2.3.0 版本中引入,广泛用于主题开发、REST API 和后台管理功能中。

代码示例

function is_taxonomy_hierarchical( $taxonomy ) {
    if ( ! taxonomy_exists( $taxonomy ) ) {
        return false;
    }

    $taxonomy = get_taxonomy( $taxonomy );
    return $taxonomy->hierarchical;
}

注意事项

  • 使用前应确保分类法已正确注册,否则可能返回 false 导致误判。
  • 该函数常用于条件标签中,开发者可参考主题开发手册中的 Conditional Tags 文章了解更多类似函数。

📄 原文内容

Determines whether the taxonomy object is hierarchical.

Description

Checks to make sure that the taxonomy is an object first. Then Gets the object, and finally returns the hierarchical value in the object.

A false return value might also mean that the taxonomy does not exist.

For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.

Parameters

$taxonomystringrequired
Name of taxonomy object.

Return

bool Whether the taxonomy is hierarchical.

Source

function is_taxonomy_hierarchical( $taxonomy ) {
	if ( ! taxonomy_exists( $taxonomy ) ) {
		return false;
	}

	$taxonomy = get_taxonomy( $taxonomy );
	return $taxonomy->hierarchical;
}

Changelog

Version Description
2.3.0 Introduced.