WooCommerce-有条件的has_term在更新后不再起作用 [英] WooCommerce - Conditional has_term doesn't work anymore after updating
问题描述
So I'm using the conditional of tho code Snippet from this example, with this thread code with success:
但是更新我的WordPress Core和WooCommerce插件后,它不再起作用。
BUT It's no longer working after updating my WordPress Core and WooCommerce Plugin.
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>';
}
}
Child Plugin still在function.php文件中具有正确的代码。
Child Plugin still has the proper code in the function.php file.
请问如何解决这个问题?
How can I solve this issue please?
谢谢
推荐答案
尝试这种方式,使用$ post全局对象并将条件嵌入到函数中:
Try this way may be, using $post global object and embedding the conditional inside your function:
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全局对象之后的函数中。
The conditional has_term() needs sometimes it's third argument to work… In function.php it can't find the current post So in this case is better to embed it inside the function after $post or $product global object.
这篇关于WooCommerce-有条件的has_term在更新后不再起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!