我有一个选项卡集来添加一个包含 WooCommerce 规范的选项卡。我想将它包装到一个 if 语句中,以便仅在产品属于某个类别时才设置选项卡。
add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
function woo_custom_product_tabs( $tabs ) {
global $post;
if ($product->is_category("Mobile Phones")) {
$tabs['custom_specification'] = array( 'title' => __( 'Specification', 'woocommerce' ), 'priority' => 50, 'callback' => 'woo_custom_specification_content' );
}
}
检查 if 语句括号中 WooCommerce 类别的正确代码是什么?
最佳答案
如果您在类别存档页面上,条件 is_category()
将返回 true。
由于您需要单个产品页面的条件,因此您将以 is_product()
条件组合的方式定位单个产品页面:
if ( is_product() && has_term( 'Mobile Phones', 'product_cat' ) ) {
$tabs['custom_specification'] = array( 'title' => __( 'Specification', 'woocommerce' ), 'priority' => 50, 'callback' => 'woo_custom_specification_content' );
}
或者你也可以尝试,以防万一,这个也是:
if( is_product() && has_category( 'Mobile Phones' ) ) {
$tabs['custom_specification'] = array( 'title' => __( 'Specification', 'woocommerce' ), 'priority' => 50, 'callback' => 'woo_custom_specification_content' );
}
@edit:您在函数末尾最后一个右括号 }
之前错过了 return $tabs;
/strong>.
引用资料:
关于php - 如果产品属于特定类别 WooCommerce,我如何设置选项卡(仅),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38613769/