daily pastebin goal
47%
SHARE
TWEET

WooCommerce - Add cat ids to product page body classes

lorro Oct 23rd, 2017 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top