Display a specific hierarchical level of a specific custom taxonomy

admin2025-06-06  8

How do I adapt the following snippet to apply to a specific custom taxonomy? It current applies to post categories

I use the function below with this: <?php display_cat_level(X,true); ?> in my theme files to display each hierarchical category level

function get_level($category, $level = 0)
{
    if ($category->category_parent == 0) {
        return $level;
    } else {
        $level++;
        $category = get_category($category->category_parent);
        return get_level($category, $level);
    }
}

function display_cat_level( $level = 0 , $link=false){

    $cats = get_the_category( );
    if( $cats ){
        foreach($cats as $cat){
            $current_cat_level = get_level($cat);
            if( $current_cat_level  == $level ){
                if($link==true) {
                    echo '<a href="'.get_category_link($cat->cat_ID).'">'.$cat->name."</a>";
                } else {
                    echo $cat->name."";
                }
            }
        }
    }
}

Answer: These updates from Jacob allows the original and updated functions to work together...

<?php 
function get_level_subject($category, $level = 0)
{
    if ($category->parent == 0) {
        return $level;
    } else {
        $level++;
        $category = get_term( $category->parent );
        return get_level_subject($category, $level);
    }
}

function display_cat_level_subject( $level = 0 , $link=false){

    $cats = get_the_terms( null, 'subject' );
    if( $cats ){
        foreach($cats as $cat){
            $current_cat_level = get_level_subject($cat);
            if( $current_cat_level  == $level ){
                if($link==true) {
                    echo '<a href="'.get_term_link( $cat->term_id ).'">'.$cat->name."</a>";
                } else {
                    echo $cat->name."";
                }
            }
        }
    }
} 
?>

How do I adapt the following snippet to apply to a specific custom taxonomy? It current applies to post categories

I use the function below with this: <?php display_cat_level(X,true); ?> in my theme files to display each hierarchical category level

function get_level($category, $level = 0)
{
    if ($category->category_parent == 0) {
        return $level;
    } else {
        $level++;
        $category = get_category($category->category_parent);
        return get_level($category, $level);
    }
}

function display_cat_level( $level = 0 , $link=false){

    $cats = get_the_category( );
    if( $cats ){
        foreach($cats as $cat){
            $current_cat_level = get_level($cat);
            if( $current_cat_level  == $level ){
                if($link==true) {
                    echo '<a href="'.get_category_link($cat->cat_ID).'">'.$cat->name."</a>";
                } else {
                    echo $cat->name."";
                }
            }
        }
    }
}

Answer: These updates from Jacob allows the original and updated functions to work together...

<?php 
function get_level_subject($category, $level = 0)
{
    if ($category->parent == 0) {
        return $level;
    } else {
        $level++;
        $category = get_term( $category->parent );
        return get_level_subject($category, $level);
    }
}

function display_cat_level_subject( $level = 0 , $link=false){

    $cats = get_the_terms( null, 'subject' );
    if( $cats ){
        foreach($cats as $cat){
            $current_cat_level = get_level_subject($cat);
            if( $current_cat_level  == $level ){
                if($link==true) {
                    echo '<a href="'.get_term_link( $cat->term_id ).'">'.$cat->name."</a>";
                } else {
                    echo $cat->name."";
                }
            }
        }
    }
} 
?>
Share Improve this question edited Nov 23, 2018 at 12:31 Pete asked Nov 23, 2018 at 11:41 PetePete 1,0582 gold badges14 silver badges40 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Change:

  • get_the_category() to get_the_terms( null, 'taxonomy_name' ).
  • get_category_link( $cat->cat_ID ) to get_term_link( $cat->term_id ).
  • get_category( $category->category_parent ) to get_term( $category->parent ).

If you want to use the same function for multiple taxonomies, you can accept the taxonomy name as an argument and pass it to the first item above:

function display_cat_level( $level = 0 , $link = false, $taxonomy = 'category' ){
    $cats = get_the_terms( null, $taxonomy );
    // etc.
}

Also, even when working with categories, don't use cat_ID and category_parent. Those were deprecated 11 years ago in favour of term_id and parent.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749156773a316847.html

最新回复(0)