[Resolved] Function adding body class to custom post type breaks GP generated body classes

Home Forums Support [Resolved] Function adding body class to custom post type breaks GP generated body classes

Home Forums Support Function adding body class to custom post type breaks GP generated body classes

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #2040257
    Pat

    Hi,
    Weird problem and I’m hoping you can help! I added a custom function (via functions.php in a GP child theme) to dynamically add a body class based on a custom post type taxonomy. The purpose: I’m using the Modern Events Calendar and I want to mark the Events tab (and its event-related children) as “current” or “ancestor” – and remove the “current” styling on the blog tab . The following function works great:

    function custom_body_classes( $classes ) {
    if ( is_single() ) {
    global $post;
    foreach ( ( get_the_terms( $post->ID, 'mec_category' ) ) as $term) { 
    $classes[] = $term->slug;
    }
    }
    return $classes;
    }
    add_filter( 'body_class', 'custom_body_classes' );

    BUT as soon as I load a non-Event page on the front end, this PHP error is being generated:
    Invalid argument supplied for foreach()

    THIS modification to the custom function works & resolves the error BUT it’s interfering with the GP generated body classes:

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

    This is the list of body classes for the blog when the first custom function is in place:
    blog logged-in wp-custom-logo wp-embed-responsive post-image-below-header post-image-aligned-center slideout-enabled slideout-mobile sticky-menu-fade right-sidebar nav-float-right one-container nav-search-enabled header-aligned-right dropdown-hover

    And this is the list of body classes for the blog when the second / revised function is on place:
    right-sidebar nav-float-right one-container nav-search-enabled header-aligned-right dropdown-hover

    Any thoughts about why this is happening and suggestions for tweaking the function so it doesn’t bork the GP generated classes?

    Thanks!
    pk

    #2040655
    Elvin
    Staff
    Customer Support

    Hi pk,

    We may need to modify the top most condition a bit to check if the single post actually has the taxonomy first AND then get the taxonomy’s terms if the single post has one.

    Example:

    function custom_body_classes( $classes ) {
        global $post;
    
        if ( is_single() && has_term('', 'mec_category') ) {
            $my_terms = get_the_terms( $post->ID, 'mec_category' );  
            foreach ( $my_terms as $term ) {
                $classes[] = $term->slug;
            }
        }
        return $classes;
    }
    add_filter( 'body_class', 'custom_body_classes' );

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

    #2040672
    Pat

    Thanks Elvin! You are a star! Works like a charm. I have a lifetime GP pro license but if I want to make a donation to show my support is there a way to do that? Never mind – I found the donation form!
    pk

    #2040679
    Elvin
    Staff
    Customer Support

    Thanks Elvin! You are a star! Works like a charm. I have a lifetime GP pro license but if I want to make a donation to show my support is there a way to do that? Never mind – I found the donation form!
    pk

    No problem. And thanks! We appreciate it.

    Stay safe. πŸ™‚

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

    #2041910
    Tom
    Lead Developer
    Lead Developer

    Thanks so much for the donation! Much appreciated πŸ™‚

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