WooCommerce - 条件 has_term 在更新后不再起作用
WooCommerce - Conditional has_term doesn't work anymore after updating
所以我成功地使用了这个 example, with 中的代码片段的条件:
但是在更新我的 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 文件中仍有正确的代码。
请问如何解决这个问题?
谢谢
尝试这种方式可能是,使用 $post 全局对象并将条件嵌入函数中:
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>';
}
};
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.
所以我成功地使用了这个 example, with
但是在更新我的 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 文件中仍有正确的代码。
请问如何解决这个问题?
谢谢
尝试这种方式可能是,使用 $post 全局对象并将条件嵌入函数中:
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>';
}
};
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.