简体   繁体   English

如何从functions.php中的Shortcode Hook中排除产品类别ID - WooCommerce

[英]How to exclude Product Category ID from Shortcode Hook inside functions.php - WooCommerce

I am trying to exclude/disable a shortcode that I've added to the Single Product Template of woocommerce for specific product categories.我正在尝试排除/禁用我为特定产品类别添加到 woocommerce 的单一产品模板的短代码。

The code in functions.php is this:在functions.php中的代码是这样的:

add_action( 'woocommerce_single_product_summary', 'sizeguidemen', 20 );
 
function sizeguidemen() {
    echo do_shortcode('[elementor-template id="66083"]');
}

How can we exclude/disable the shortcode for specific product categories?我们如何排除/禁用特定产品类别的短代码?

I believe it should be something with array( 'product_cat', '65' );我相信它应该与array( 'product_cat', '65' ); ? ?

I'm new to php and trying to do it, but doesn't work.我是 php 的新手并试图这样做,但不起作用。 :/ :/

Conditional logic allows you to run your functions only when certain criteria is met.条件逻辑允许您仅在满足特定条件时运行您的函数。

In your case, you're looking at the single product page and need to run a function only when category is 'whatever ID'.在您的情况下,您正在查看单个产品页面,并且仅当类别为“任何 ID”时才需要运行功能。

By looking at WooCommerce Conditional Logic – Tags, Examples & PHP tutorial, and specifically at the "PHP: do something if product belongs to a category" section, your code would become the following, where "123" is the category ID you want that to work for:通过查看WooCommerce 条件逻辑 – 标签、示例和 PHP教程,特别是“PHP:如果产品属于某个类别,则执行某些操作”部分,您的代码将变为以下内容,其中“123”是您想要的类别 ID为:

add_action( 'woocommerce_single_product_summary', 'sizeguidemen', 20 );
 
function sizeguidemen() {
    if ( has_term( 123, 'product_cat' ) ) {
        echo do_shortcode('[elementor-template id="66083"]');
    }
}

You can even use category "slug" instead of "123", and also an array of these eg [123, 456, 789] or ['tables', 'chairs']您甚至可以使用类别“slug”而不是“123”,以及这些的数组,例如 [123, 456, 789] 或 ['tables', 'chairs']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM