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

如何实现仅在Wallpaper产品分类下显示Order Sample按钮?

Solution: Display "Order Sample" Button Only for Wallpaper Category Products

Got it, let's fix this up so that the Order Sample button only shows on products in your Wallpaper category. Your current approach was modifying product shortcode queries, which isn't the right way to control button visibility. Instead, we'll check the product's category directly before rendering the button.

Step 1: Replace Your Existing Code

Remove the exclude_cat_shortcodes function entirely, and update the content_after_addtocart_button function to include a category check:

add_action( 'woocommerce_after_add_to_cart_form', 'content_after_addtocart_button' );
function content_after_addtocart_button() {
    // Get the current product object
    global $product;

    // Check if the product belongs to the "wallpaper" category (by slug)
    if ( has_term( 'wallpaper', 'product_cat', $product->get_id() ) ) {
        echo '<div style="clear: both; margin-bottom: 30px"></div> 
        <div class="product-buttons"> 
            <a href="/wallpaper-samples/">Order Sample</a>
        </div>';
    }
}

How This Works

  • Global Product Object: We use global $product to access the current product's data, which works perfectly even with your Permalink plugin changes (since we're checking the product's actual category attributes, not the URL structure).
  • Category Check: The has_term() function verifies if the product is assigned to the category with slug wallpaper. Adjust this slug if your category uses a different one (you can confirm the slug in WordPress Admin → Products → Categories).
  • Conditional Rendering: The button HTML only outputs if the category check passes—so it won't appear on any products outside the Wallpaper category.

Quick Notes

  • If you ever need to target multiple categories, just pass an array of slugs to has_term(), like array('wallpaper', 'custom-wallpapers').
  • The styling for the button stays exactly as you had it, so no need to adjust your CSS unless you want to tweak the appearance later.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 08:54:47