Advertisement
lorro

WooCommerce - Add cat ids to product page body classes

Oct 23rd, 2017
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.54 KB | None | 0 0
  1. <?php
  2. // add category identifiers in the body classes for product pages
  3. add_filter( 'body_class', 'add_body_classes' );
  4. function add_body_classes( $classes ) {
  5.   global $post;
  6.   // product pages only
  7.   if ( is_product() ) {
  8.     // get the categories for this product
  9.     $terms = get_the_terms( $post->ID, 'product_cat' );
  10.     foreach ($terms as $term) {
  11.       // you can use either slug or id, no point having both
  12.       $classes[] = 'cat_slug_'.$term->slug;
  13.       $classes[] = 'cat_id_'.$term->term_id;
  14.     }
  15.     return $classes;
  16.   }
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement