我在我的功能中为我的产品页面添加了其他选项卡。php文件,现在需要将其包装在if语句中,以便其他选项卡仅显示在“模型”类别内的产品上我已经尝试了我能找到的每一种方法,但都不管用。它们都会去掉每个页面上的附加选项卡,即使它属于指定的类别。
我试过上面提到的所有东西this question, 我假设他们都没有工作过,因为那个人正在编辑他们的单个产品。php文件,我正在编辑我的函数。php文件?
if ( is_product() && has_term( \'Models\', \'product_cat\' ) ) {
////Add Custom Tabs
add_filter( \'woocommerce_product_tabs\', \'woo_new_product_tab\' );
function woo_new_product_tab( $tabs ) {
// Adds a compare tab
$tabs[\'compare\'] = array(
\'title\' => __( \'Compare\', \'woocommerce\' ),
\'id\' => \'compare\',
\'priority\' => 50,
\'callback\' => \'woo_compare_tab_content\'
);
// Adds a warranty tab
$tabs[\'warranty\'] = array(
\'title\' => __( \'Warranty\', \'woocommerce\' ),
\'id\' => \'warranty\',
\'priority\' => 50,
\'callback\' => \'woo_warranty_tab_content\'
);
return $tabs;
}
function woo_warranty_tab_content() {
$warranty = get_post_meta( get_the_ID(), \'wpcf-warranty\', true );
// Warranty Tab Content
echo \'<div class="fusion-title title sep-double">\';
echo \'<h3 class="title-heading-left">Warranty</h3>\';
echo \'<div class="title-sep-container"><div class="title-sep sep-double"></div></div>\';
echo \'</div>\';
echo "$warranty";
}
function woo_compare_tab_content() {
$compare = get_post_meta( get_the_ID(), \'wpcf-compare\', true );
// Comparison Tab Content
echo \'<div class="fusion-title title sep-double">\';
echo \'<h3 class="title-heading-left">Compare</h3>\';
echo \'<div class="title-sep-container"><div class="title-sep sep-double"></div></div>\';
echo \'</div>\';
echo "$compare";
}
}
最合适的回答,由SO网友:Jeremy Ross 整理而成
将条件添加到函数中,而不是包装挂钩和函数。我很确定你需要传递帖子ID,因为这可能没有在循环中运行???(请检查一下)。您也可以放弃函数内部的is\\u product()检查,因为挂钩仅由woocommerce触发。
function woo_new_product_tab( $tabs ) {
global $post;
if ( has_term( \'Models\', \'product_cat\', $post->ID ) ) {
// Adds a compare tab
$tabs[\'compare\'] = array(
\'title\' => __( \'Compare\', \'woocommerce\' ),
\'id\' => \'compare\',
\'priority\' => 50,
\'callback\' => \'woo_compare_tab_content\'
);
// Adds a warranty tab
$tabs[\'warranty\'] = array(
\'title\' => __( \'Warranty\', \'woocommerce\' ),
\'id\' => \'warranty\',
\'priority\' => 50,
\'callback\' => \'woo_warranty_tab_content\'
);
}
return $tabs;
}
您可以在此处找到更多解释:
https://bloke.org/wordpress/conditional-woocommerce-product-tabs/