隐藏特定页面 WooCommerce 描述和其他信息和评论选项卡使用 WordPress 自定义字段



我正在尝试创建一个系统,通过该系统,我可以删除产品选项卡,但只能在某些单个产品页面上删除,并且我需要使用wordpress自定义字段定义哪些页面的产品选项卡要隐藏。我要调用的自定义字段名称:"hide_product_page_tabs",定义值需要为"1"或"0"(表示是或否(。

我在我选择的wooccommerce产品页面上创建了一个新的Wordpress自定义字段。

自定义字段名称:hide_product_tabs

自定义字段值:在自定义字段中定义一个"1"来触发代码,或定义任何其他值(如"0"(来关闭代码。

我在我的孩子主题的函数中放置了.pp:

/* WooCommerce hide product page tabs - hide_product_tabs */
/**
* Remove existing tabs from single product pages.
* https://gist.github.com/mikejolley/c75083db7f6110cbdbe4808e3af36fe3
*/
function remove_woocommerce_product_tabs( $tabs ) {
unset( $tabs['description'] );
unset( $tabs['reviews'] );
unset( $tabs['additional_information'] );
return $tabs;
}
function hide_product_page_tabs() {
global $post;
$product_id = $post->ID;
$HideProductTabsValue =  get_post_meta($product_id,'hide_product_tabs',true);
if (strpos($HideProductTabsValue, '1') !== false) {
return add_filter( 'woocommerce_product_tabs', 'remove_woocommerce_product_tabs', 98 );
}
}       
add_action('woocommerce_single_product_summary','hide_product_page_tabs');

欢迎任何提示!

根据您的描述,在没有看到您为此使用的代码的情况下,您可以简单地使用以下

function hide_product_tabs( $tabs ) {
// Get the global product object
global $product;
// Get product id
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
$HideProductTabsValue = get_post_meta( $product_id, 'hide_product_tabs', true);
// 1 = true
if( $HideProductTabsValue == true ) {
unset( $tabs['description'] ); // (Description tab)  
unset( $tabs['reviews'] ); // (Reviews tab)
unset( $tabs['additional_information'] ); // (Additional information tab)       
}
return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'hide_product_tabs', 98 );

最新更新