I'm making a dynamic galery of products with categories using Isotope and Woocommerce on wordpress , to sort the products I use their category slug but when a product has more than one category I cant find a way to get the other category and at this point I'm not even sure if this is possible .
I've tried to get the categories instead of the slug with $product->get_categories();
but it returns an <a href>
with the categories in it .
function gallery(){
Print "<div class='button-group filter-button-group'>
<button data-filter='*' >show all</button>
<button data-filter='.music-album'>Music</button>
<button data-filter='.movie-soundtrack'>Movies</button>
<button data-filter='.game-soundtrack'>Games</button>
</div>";
Print"<div class='row'>";
$products= new WP_Query(['post_type'=>'product']);
if($products->have_posts()) : while ($products->have_posts()) : $products->the_post();
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
$single_cat = array_shift( $product_cats );
$category = $single_cat->slug.' '; // IF POSSIBLE : I want here to get all the slug of the actual product on the loop
Print"<a class='grid-item ".$category."' href='".get_the_permalink()."'>";
Print"<div class='card grid-item".$category."'>";
Print"<img class='card-img-top'src='".get_field('image')."'alt='' width='200' height='200'>";
Print"<h1 class='card-body'>"; the_title(); Print"</h1>";
Print"</div>";
Print"</a>";
endwhile;endif;
Print"</div>";
}
So idealy I want the $category variable to be equal to something like category_slug1 category_slug2 category_slug3 ....
and if the product have 10 categories I want it to find them all .
$slugs = array();
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
foreach ($product_cats as $product_cat){
$slugs[] = $product_cat->slug;
}
This will add all slugs into slugs array. You can then use implode() to glue it to a string.