2016-08-25 36 views
1

所以我使用条件寿代码片断从这个example后的成功合作,与this thread codeWooCommerce - 条件has_term已经不更新

但它更新我的WordPress核心和WooCommerce后它不再工作插入。

if (is_product() && has_term('sample-category', 'product_cat')){ 

    add_action('woocommerce_after_add_to_cart_button', 'add_custom_button', 10, 0); 
    function add_custom_button() { 
     global $products; 

     $product_link = get_permalink($products->id); 
     $sample_link = substr($product_link, 0, -1) . '-swatch-card/'; 
     echo '<a class="button alt btn-sample" href="' . esc_url($sample_link) .'">' . __("Order a Sample", "my_theme_slug") . '</a>'; 

    } 

} 

子插件仍然在function.php文件中有正确的代码。

我该如何解决这个问题?

感谢

回答

2

尝试这种方式,可以使用$后全局对象和嵌入条件的函数中:

add_action('woocommerce_after_add_to_cart_button', 'add_custom_button', 10, 0); 
function add_custom_button() { 
    global $post; 
    if (has_term('collection', 'product_cat', $post->ID)) { 
     $product_link = get_permalink($post->ID); 
     $sample_link = substr($product_link, 0, -1) . '-swatch-card/'; 
     echo '<a class="button alt btn-sample" href="' . esc_url($sample_link) .'">' . __("Order a Sample", "my_theme_slug") . '</a>'; 
    } 
}; 

的条件has_term()的需求有时是第三个参数来工作...在function.php中找不到当前帖子所以在这种情况下,最好将它嵌入到$ post或$ product全局对象之后的函数中。