daily pastebin goal
12%
SHARE
TWEET

WooCommerce - Insert category name(s) before product

lorro Jul 29th, 2015 (edited) 371 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.   // insert category name(s) before product in shop loop
  3.   // products can have none, one or many categories
  4.   // code goes in functions.php for your child theme
  5.   add_action('woocommerce_before_shop_loop_item', 'insert_cat_names');
  6.   function insert_cat_names() {
  7.     $terms = get_the_terms($product->ID, 'product_cat');
  8.     $nr_terms = count($terms);
  9.     if ($nr_terms == 1) {
  10.       print '<p class="cat_names">Category: '.$terms[0]->name.'</p>';
  11.     }
  12.     if ($nr_terms > 1) {
  13.       print '<p class="cat_names">Categories: ';
  14.       $first = true;
  15.       foreach ($terms as $term) {
  16.         if ($first) {
  17.           $first = false;
  18.         } else {
  19.           print ', ';
  20.         }
  21.         print $term->name;
  22.       }
  23.       print '</p>';
  24.     }
  25.   }
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