daily pastebin goal
68%
SHARE
TWEET

WooCommerce - Add gallery image to products in a category

lorro Dec 28th, 2015 (edited) 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.   // add a gallery image to all products in a specific category
  3.   // code goes in functions.php for your child theme
  4.   // in this example, 12 is the category id and 2931 is the media library image id.
  5.   add_filter('woocommerce_product_gallery_attachment_ids', 'add_gallery_image', 10, 2);
  6.   function add_gallery_image($ids, $product) {
  7.     $terms = get_the_terms( $product->post->ID, 'product_cat' );
  8.     foreach ($terms as $term) {
  9.       if ($term->term_id == 12) {
  10.         $ids[] = '2931';
  11.         break;
  12.       }
  13.     }
  14.     return $ids;
  15.   }
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