$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Display a specific hierarchical level of a specific custom taxonomy|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Display a specific hierarchical level of a specific custom taxonomy

matteradmin10PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far