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
1 Answer
Reset to default 1Change:
get_the_category()
toget_the_terms( null, 'taxonomy_name' )
.get_category_link( $cat->cat_ID )
toget_term_link( $cat->term_id )
.get_category( $category->category_parent )
toget_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
.