【发布时间】:2020-11-22 21:07:48
【问题描述】:
我使用这个 sn-p 在商店和档案页面的产品循环中显示产品类别:
add_action( 'woocommerce_after_shop_loop_item_title', 'add_product_cat', 2);
function add_product_cat()
{
global $product;
$product_cats = wp_get_post_terms($product->id, 'product_cat');
$count = count($product_cats);
foreach($product_cats as $key => $cat)
{
echo '<span class="add_product_cat">' . $cat->name . '</span>';
}
}
我想要这个:
<div class="product_cats">
<span class="add_product_cat">Cat 1</span>
<span class="add_product_cat">Cat 2</span>
</div>
为此,我使用 jQuery:
jQuery(function(){
jQuery(".woocommerce li.product").each(function() {
jQuery(this).find('span.add_product_cat').wrapAll('<div class="product_cats"></div>');
});
});
如何修改我的 PHP sn-p 以避免使用 jQuery?
【问题讨论】:
标签: php wordpress loops woocommerce product