[Resolved] Add Taxonomy to body Class

Home Forums Support [Resolved] Add Taxonomy to body Class

Home Forums Support Add Taxonomy to body Class

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1524362
    Dan

    I wanted to add the taxonomy to the body class of single pages.
    I added this function to functions.php.
    It does add the taxonomy to single but strips away other classes in the category pages, thus eliminating the sticky header.

    This is the function:

    function add_taxonomy_to_single( $classes ) {
        if ( is_single() ) {
            global $post;
            $my_terms = get_the_terms( $post->ID, 'brand' );
            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' );

    This is the link to the single page (sticky menu is working):
    http://trm.ojs.mybluehost.me/furniture/chairs/bella-850-2/

    This is the link to the category page (sticky menu stopped working):
    http://trm.ojs.mybluehost.me/furniture/brand/chairs/

    Any idea why the function is causing issue with the body class?

    Thanks,
    Dan

    #1524433
    Elvin
    Staff
    Customer Support

    Hi,

    Can you try this:

    function add_taxonomy_to_single( $classes ) {
        if ( is_single() ) {
            global $post;
            $my_terms = get_the_terms( $post->ID, 'brand' );
            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' );

    A wise man once said:
    "Have you cleared your cache?"

    #1524463
    Dan

    Thanks Elvin, that did it!

    Dan

    #1524619
    Elvin
    Staff
    Customer Support

    Nice one. No problem.

    A wise man once said:
    "Have you cleared your cache?"

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.