Skip to content

Instantly share code, notes, and snippets.

@Langmans
Created October 7, 2015 09:15
Show Gist options
  • Save Langmans/7ab8a8d1687fe691cb7b to your computer and use it in GitHub Desktop.
Save Langmans/7ab8a8d1687fe691cb7b to your computer and use it in GitHub Desktop.
add all woocommerce product categories to body_class
<?php
// add all woocommerce product categories to body_class
function product_category_body_classes( $classes ){
if( is_singular( 'product' ) || is_tax( 'product_cat' ) ) {
foreach (get_the_terms(0, 'product_cat') as $custom_term) {
do {
$classes[] = 'product-category-' . $custom_term->slug;
//if( is_singular( 'product' ) ) {
$classes[] = 'term-' . $custom_term->term_id;
//}
// Get the parent product category:
$custom_term = $custom_term->parent ? get_term( $custom_term->parent, 'product_cat' ) : null;
//var_dump($custom_term);
} while ($custom_term);
}
}
return $classes;
}
add_filter( 'body_class', 'product_category_body_classes' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment