Stampare la categoria prodotto di woocommerce come classe nel body quando visiti un prodotto singolo

Con questa funzione puoi stampare tra le classi del body la categoria del prodotto singolo di woocommerce. Per funzionare basta copiare e incollare la funziona nel file functions.php

function sg_wooc_custom_taxonomy_in_body_class( $classes ){
  if( is_singular( 'product' ) ) {
    $custom_terms = get_the_terms(0, 'product_cat');
    if ($custom_terms) {
      foreach ($custom_terms as $custom_term) {
        $classes[] = 'term-' . $custom_term->slug;
      }
    }
  }
  return $classes;
}

add_filter( 'body_class', 'sg_wooc_custom_taxonomy_in_body_class' );

Stampare la categoria come classe nel body quando visiti un post singolo

Una funzione semplice che aiuta a stampare tra le classi del body la categoria del post singolo. Per funzionare basta copiare e incollare nel file functions.php

function sg_add_category_to_single($classes) {
    if (is_single() ) {
      global $post;
      foreach((get_the_category($post->ID)) as $category) {
            // add category slug to the $classes array
            $classes[] = $category->category_nicename;
      }
    }
    // return the $classes array
    return $classes;
}

add_filter('body_class','sg_add_category_to_single');