【发布时间】:2019-12-13 22:16:28
【问题描述】:
我正在 wordpress 上使用 Isotope 和 Woocommerce 制作具有类别的产品动态图库,以对我使用其类别 slug 的产品进行排序,但是当产品具有多个类别时,我无法找到获取其他类别的方法,并且在这一点上,我什至不确定这是否可能。
我尝试使用 $product->get_categories(); 获取类别而不是 slug,但它返回一个包含类别的 <a href>。
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>";
}
所以理想情况下,我希望 $category 变量等于 category_slug1 category_slug2 category_slug3 .... 之类的值,如果产品有 10 个类别,我希望它能够找到所有类别。
【问题讨论】:
标签: php woocommerce word slug jquery-isotope