Skip to content

Instantly share code, notes, and snippets.

@mio31337
Last active April 14, 2020 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mio31337/2bacd581fa008e125f984d841b3b8830 to your computer and use it in GitHub Desktop.
Save mio31337/2bacd581fa008e125f984d841b3b8830 to your computer and use it in GitHub Desktop.
WooCommerce: Add Product Cateogry class to body on single Product page
/**
* Add Product Cateogry class to body on single Product page
* Author: MarioMarkovic.com
*/
add_filter( 'body_class', 'wc_product_cats_css_body_class' );
function wc_product_cats_css_body_class( $classes ){
if ( is_singular( 'product' ) ) {
$current_product = wc_get_product();
$custom_terms = get_the_terms( $current_product->get_id(), 'product_cat' );
if ( $custom_terms ) {
foreach ( $custom_terms as $custom_term ) {
$classes[] = 'product_cat_' . $custom_term->slug;
}
}
}
return $classes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment