Php Wordpress,在归档页面中获取当前分类级别

Php Wordpress,在归档页面中获取当前分类级别,php,wordpress,taxonomy,custom-taxonomy,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 { // sh

我正在创建一个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
}

有人能解释一下如何让
$current\u term\u level
输出适当的值吗?

我已经设法让它这样工作:

$current_term = get_queried_object()->slug;
$tax_name = 'items';
$terms = get_terms( $tax_name );
foreach($terms as $term) {
    $parent = get_term($term->parent, $tax_name);
    $grandparent = get_term($parent->parent, $tax_name);
    $great_grandparent = get_term($grandparent->parent, $tax_name);
    if ($term->slug == $current_term) {
        if ($term->parent == 0) {
            echo 'top level';
        } else if ($parent->parent == 0) {
            echo 'second level';
        } else if ($grandparent->parent == 0) {
            echo 'third level';
        } else if ($great_grandparent->parent == 0) {
            echo 'fourth level';
        }
    }
}
我知道这不是最干净的解决办法。它工作正常,因为我有有限数量的分类法子级别,但如果能看到它使用递归得到回答,那就太好了。也许有人会觉得这很有帮助。

尝试使用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
}