You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改自定义文本代码使其仅显示在指定WooCommerce分类(如Tables分类)中

How to Show Custom Text Only in a Specific WooCommerce Category (Tables)

Got it, let's tweak your existing code so that the "Meer varianten" label only shows up on variable products within the Tables category. Here's the straightforward solution:

Modified PHP Code

Replace your current PHP snippet with this version — it adds targeted checks for both the product type and your desired category:

add_action( 'woocommerce_after_shop_loop_item', 'show_meer_varianten_loop', 100 );
function show_meer_varianten_loop() {
    // Grab the current product from the shop loop
    global $product;
    
    // Only render the label if two conditions are met:
    // 1. The product is a variable product
    // 2. The product is in the 'Tables' category (use your category's slug here)
    if ( $product->is_type('variable') && has_term( 'tables', 'product_cat', $product->get_id() ) ) {
        ?>
        <div class="custom-label-variable">✓ Meer varianten</div>
        <?php
    }
}

Important Details:

  • Category Slug: Double-check that 'tables' matches the actual slug of your "Tables" category (you can find this in WooCommerce > Products > Categories, under the "Slug" column). If your category has a different slug (like 'dining-tables'), update that value accordingly.
  • Product Type Filter: We added the $product->is_type('variable') check directly in PHP, which is more efficient than relying solely on CSS to hide the label for non-variable products.

CSS Adjustment

You can keep your existing CSS as a safe fallback (it won't cause any issues), but since we're already filtering products in PHP, the label won't even be output for non-qualifying products anymore. If you want to clean things up, you can remove this CSS entirely:

.products .product:not(.product-type-variable) .custom-label-variable { display: none; }

内容的提问来源于stack exchange,提问作者Helpmeplease

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 12:02:36