0

How would this be adapted to apply to specific custom taxonomies?

add_filter( 'body_class', 'custom_cat_archiev_class' );
function custom_cat_archiev_class( $classes ) {
    if ( is_category() ) {
        $cat = get_queried_object();
        $ancestors = get_ancestors( $cat->term_id, 'category', 'taxonomy' );
        $classes[] = 'catlevel-' . ( count( $ancestors ) + 1 );
    }
    return $classes;
}

1 Answer 1

0

Use is_tax instead of is_category, and update get_ancestors to get taxonomy from the queried object:

add_filter( 'body_class', 'custom_cat_archiev_class' );
function custom_cat_archiev_class( $classes ) {
    if ( is_tax( ['custom_tax_1', 'custom_tax_2'] ) ) {
        $term = get_queried_object();
        $ancestors = get_ancestors( $term->term_id, $term->taxonomy, 'taxonomy' );
        $classes[] = 'catlevel-' . ( count( $ancestors ) + 1 );
    }
    return $classes;
}
2
  • It works, but I can't update my upvote.
    – Pete
    Nov 26, 2018 at 11:15
  • 1
    Never vote in a hurry. +1 from me.
    – Rahul
    Nov 27, 2018 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.