【问题标题】:Creating condtional shortcode echo - WP/Woocommerce创建条件短代码回显 - WP/Woocommerce
【发布时间】:2020-06-12 09:23:28
【问题描述】:

我创建了一堆短代码,它们要么在帖子中使用,要么使用 wordpress 挂钩回显到产品页面中。

我正在尝试创建一个条件函数,以便它根据产品类别回显特定的短代码。它需要回显到“woocommerce_after_single_product_summary”中。

这是目前为止的草稿,有人可以就如何完成这个提供一些建议吗?

add_action( 'woocommerce_after_single_product_summary', 'add_warranty_notice', 15 );

function add_warranty_notice() {

if ( is_product_category( 'xx-slug' ) ) {
echo do_shortcode("[xx-product-notice]");
} elseif ( is_product_category( 'xy-slug' ) ) {
echo do_shortcode("[xy-product-notice]");
}

}

【问题讨论】:

  • 解释一下究竟什么还需要完成?你所展示的东西让你走了多远,到底还缺少什么?
  • 因此我们的想法是根据产品类别将短代码回显到“woocommerce_after_single_product_summary”中的页面中。

标签: php wordpress woocommerce conditional-statements hook-woocommerce


【解决方案1】:

希望对您有所帮助。

add_action( 'woocommerce_after_single_product_summary', 'add_warranty_notice', 15 );

function add_warranty_notice() {
    global $post;
    $terms = get_the_terms( $post->ID, 'product_cat' );
    $nterms = get_the_terms( $post->ID, 'product_tag'  );
    foreach ($terms  as $term  ) {
        $product_cat_id = $term->term_id;
        $product_cat_name = $term->name;
        break;
    }

    if ( $product_cat_name == 'parent_one' ) {
        echo do_shortcode("[xx-product-notice]");
    } elseif ( $product_cat_name == 'parent_two' ) {
        echo do_shortcode("[xy-product-notice]");
    }else{

    }

}

【讨论】:

  • 您好,感谢您的帮助。不幸的是,这似乎不起作用。我已经尝试了类别 slug 和名称作为选项,但它仍然没有呈现。
  • 已测试它是基于 slug 的。你试过什么?
猜你喜欢
  • 2018-05-03
  • 1970-01-01
  • 2018-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-27
  • 1970-01-01
相关资源
最近更新 更多