Wordpress,在存档页面中获取当前的分类级别

kam*_*der 4 php wordpress taxonomy custom-taxonomy

我正在创建一个 WordPress 站点,以使用自定义帖子类型和自定义分层分类法显示项目目录。我想保持简单并处理单个存档页面中的项目,但我需要帮助如何确定当前显示的分类级别。基本上,我需要以下功能:

if ($current_term_level = 0) {
    // show first drop-down
} else if ($current_term_level = 1) {
    // show second drop-down
} else {
    // show third drop-down
}
Run Code Online (Sandbox Code Playgroud)

有人可以解释如何$current_term_level输出适当的值吗?

Wil*_*Ode 6

尝试使用get_ancestors()WP 功能:

function get_tax_level($id, $tax){
    $ancestors = get_ancestors($id, $tax);
    return count($ancestors)+1;
}

$current_term_level = get_tax_level(get_queried_object()->term_id, get_queried_object()->taxonomy);

if ($current_term_level = 0) {
    // show first drop-down
} else if ($current_term_level = 1) {
    // show second drop-down
} else {
    // show third drop-down
}
Run Code Online (Sandbox Code Playgroud)