0

My theme uses custom taxonomy called "kbsection" and there are lots of sections set up (instead of categories). There is also a custom post type and the posts are assigned to sections.

I need to style the header differently depending on what section(s) the post belongs to. So that the posts that belong to Section A have got a different header background than the ones belonging to Section B etc. If a post belongs to more than one section it doesn't matter which one it picks. It can always be the first one in the array.

I guess what I need for that is to get the current "section" slug and somehow add it as a body class. I've been trying lots of different methods and none seem to work for me. I've found the piece of code below, but of course that only works for categories and not for my "sections". Please help.

add_filter('body_class','add_category_to_single');
function add_category_to_single($classes, $class) {
    if (is_single() ) {
        global $post;
        foreach((get_the_category($post->ID)) as $category) {
            // add category slug to the $classes array
            $classes[] = $category->category_nicename;
        }
    }
    // return the $classes array
    return $classes;
}
2

You can follow similar approach with get_the_terms. Here is the function.

function add_taxonomy_to_single( $classes ) {
    if ( is_single() ) {
        global $post;
        $my_terms = get_the_terms( $post->ID, 'custom-taxonomy' );
        if ( $my_terms && ! is_wp_error( $my_terms ) ) {
            foreach ($my_terms as $term) {
                $classes[] = $term->slug;
            }
        }
        return $classes;
    }
}
add_filter( 'body_class', 'add_taxonomy_to_single' );

Don't forget to change the name of your custom taxonomy in above code. I used custom-taxonomy for example.

EDIT 1:

To add first taxonomy name in body class here it the updated code.

function add_taxonomy_to_single( $classes ) {
    if ( is_single() ) {
        global $post;
        $my_terms = get_the_terms( $post->ID, 'custom-taxonomy' );
        if ( $my_terms && ! is_wp_error( $my_terms ) ) {
            $classes[] = $my_terms[0]->slug;
        }
        return $classes;
    }
}
add_filter( 'body_class', 'add_taxonomy_to_single' );
  • Thank you so much. That works great. I've got 2 more questions: - Is it possible for it to output only the first slug from the array? At the minute it outputs all of them. - Is it possible to do the same thing for the archive pages (sections) apart from single posts? So that if I'm on Section A, there would be a class "sectiona" assigned to the body. Thank you – adriand Jun 30 '15 at 8:03
  • I added updated code for adding only first taxonomy to body class. Keep in mind that you cannot control the order of taxonomies, so you don't control which taxonomy will appear in body class out of many. – Robert hue Jun 30 '15 at 8:29
  • Thank you Robert. Your code works great. Regarding the second question - is there any way at all for me to distinguish which "section" page I'm on, so that I could use that in CSS to style the header for it? – adriand Jun 30 '15 at 8:41
  • WordPress by default adds taxonomy name in body classes on taxonomy archive pages. You don't need additional function for that. – Robert hue Jun 30 '15 at 8:52
  • Unfortunately my theme doesn't do that. Is there any way to add that in manually? Thank you – adriand Jun 30 '15 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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