[英]Woocommerce how to display products from specific category on archive page
[英]How to select a specific woocommerce category archive to display short description for products
我添加了以下代碼以在 Woocommerce 產品存檔頁面上顯示簡短描述:
function webroom_add_short_description_in_product_categories() {
global $product;
if ( ! $product->get_short_description() ) return;
?>
<div itemprop="description">
<?php echo apply_filters( 'woocommerce_short_description', $product->get_short_description() ) ?>
</div>
<?php
} add_action('woocommerce_after_shop_loop_item_title', 'webroom_add_short_description_in_product_categories', 5);
但是 - 我希望能夠 select 僅針對此代碼的特定產品類別,所以我可以 select 我希望顯示簡短描述的類別。 請問我該怎么做?
試試這個解決方案
add_action('woocommerce_after_shop_loop_item_title', 'webroom_add_short_description_in_product_categories');
function webroom_add_short_description_in_product_categories() {
global $product;
// get product categories by product id
$product_categories = get_the_terms( $product->get_id(), 'product_cat' );
// set default state if product has category set by you or not
$has_category = false;
// set which category to display short description
$categories = [
'Accessories',
'Bags',
'Clothing',
];
// check if product categories is in array
if ( $product_categories ) {
foreach ( $product_categories as $product_category ) {
if ( in_array( $product_category->name, $categories ) ) {
// set true if product has category set by you
$has_category = true;
break;
}
}
}
if ( ! $product->get_short_description() || ! $has_category ) return;
?>
<div itemprop="description">
<?php echo apply_filters( 'woocommerce_short_description', $product->get_short_description() ) ?>
</div>
<?php }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.