Home › Forums › Support › Function adding body class to custom post type breaks GP generated body classes
- This topic has 4 replies, 3 voices, and was last updated 2 years, 4 months ago by Tom.
-
AuthorPosts
-
December 6, 2021 at 9:40 am #2040257Pat
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-hoverAnd 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-hoverAny thoughts about why this is happening and suggestions for tweaking the function so it doesn’t bork the GP generated classes?
Thanks!
pkDecember 6, 2021 at 7:16 pm #2040655ElvinStaffCustomer SupportHi 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' );
December 6, 2021 at 7:34 pm #2040672PatThanks 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!
pkDecember 6, 2021 at 7:42 pm #2040679ElvinStaffCustomer SupportThanks 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!
pkNo problem. And thanks! We appreciate it.
Stay safe. π
December 7, 2021 at 8:18 pm #2041910TomLead DeveloperLead DeveloperThanks so much for the donation! Much appreciated π
-
AuthorPosts
- You must be logged in to reply to this topic.