【问题标题】:Adding widgets to a WooCommerce sidebar using product tags conditions使用产品标签条件将小部件添加到 WooCommerce 侧边栏
【发布时间】:2017-06-09 21:49:00
【问题描述】:

我在我的 Wordpress 网站上使用 WooCommerce 作为目录(即禁用购物车),我想根据产品是否具有特定标签将小部件添加到单个产品页面侧边栏。我尝试使用“Widget Logic”插件和条件标签,但即使我禁用了所有其他插件,我也无法让它在网站上的任何地方工作(即包括带有标签的帖子和主侧边栏 - 所以不仅仅是带有标签WooCommerce 中的产品)。

然后我添加了“PHP Code Widget”并在其中添加了以下代码:

    <?php
if (is_product_tag( 'adexample1' )) {
echo "test1";
} elseif (is_product_tag( 'adexample2' )){
echo "test2";    
} elseif (is_product_tag( 'adexample3' )){
echo "test3";  
} elseif (is_product_tag( 'adexample4' )){
echo "test4";  
} elseif (is_product_tag( 'adexample5' )){
echo "test5";  
} else {
echo "<p>test6</p>";
}
?>

我已经用标记的产品和其他产品对其进行了测试,但它们都在小部件中返回“test6”。

任何人都可以看到我做错了什么,或者产品标签是否可能由于某种原因无法工作/被识别(也许是权限?)。该问题已在默认的 2017 和基本 WooCommerce 主题中复制,因此它似乎不是主题问题?

也将不胜感激提出另一种实现方式的建议?

谢谢

【问题讨论】:

    标签: php wordpress woocommerce tags product


    【解决方案1】:

    条件 is_product_tag() 将无法获取具有产品标签的产品,因为它用于在查看产品标签存档页面时返回 true。

    要实现您想要做的事情,您需要使用 WordPress has_term() 条件函数,并为 'product_tag' 定义 $taxonomy 参数,这样:

    // Define BELOW your product tag ID, slug or name (or an array of values)
    $term = 'adexample1';
    $term2 = 'adexample2'; // ...
    
    // HERE are your conditions with your code
    if( has_term( $term, 'product_tag' ) ){
        echo "test1";
    } elseif( has_term( $term2, 'product_tag' ) ){
        echo "test1";
    } else {
        echo "<p>test6</p>";
    }
    

    这应该适用于将 $taxonomy 参数定义为 'product_cat'...

    的产品类别

    【讨论】:

    • 效果很好,您的反馈非常有帮助,谢谢!
    • @Riverbum75 不客气……那是我第一次将 has_term() 函数与 WooCommerce 产品标签一起使用……但如果它与产品类别分类法一起使用,那么它将与产品标签分类法一起使用是合乎逻辑的太……
    猜你喜欢
    • 2018-11-12
    • 2019-09-04
    • 2018-01-27
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 2020-02-14
    相关资源
    最近更新 更多