如果 WooCommerce 单个产品中的内容为空,则隐藏自定义产品选项卡
Hide custom product tab if content is empty in WooCommerce single products
我在 ACF 插件中创建了一个字段。我添加了下面的代码以将数据显示为自定义选项卡,但即使该字段为空,该选项卡也始终可见。我错过了什么?
// 1.Displays the tab on every product page
add_filter( 'woocommerce_product_tabs', 'woo_new_tab' );
function woo_new_tab( $tabs ) {
// Adds the new tab
if (!empty(get_the_content())) {
$tabs['application'] = array(
'title' => __( 'Application', 'woocommerce' ),
'priority' => 20,
'callback' => 'woo_new_tab_content'
);
return $tabs;
}
}
// the callback (refer to https://www.advancedcustomfields.com/resources/code-examples/) for more info
function woo_new_tab_content() {
// The new tab content
//Working with Array values (checkbox, select, relationship, repeater) use below
$values = get_field('application'); //field name
if($values){
echo '<ul>';
foreach($values as $value){
echo '<li>' . $value . '</li>';
}
echo '</ul>';
}
}
要在内容为空时隐藏自定义产品选项卡,请改用:
// Add a custom product tab on single product pages
add_filter( 'woocommerce_product_tabs', 'woo_new_tab' );
function woo_new_tab( $tabs ) {
$values = get_field('application');
// Adds the new tab
if ( ! empty($values) ) {
$tabs['application'] = array(
'title' => __( 'Application', 'woocommerce' ),
'priority' => 20,
'callback' => 'woo_new_tab_content'
);
}
return $tabs;
}
// Displays the tab content
function woo_new_tab_content() {
$values = (array) get_field('application');
echo '<ul>';
foreach ( $values as $value ) {
echo '<li>' . $value . '</li>';
}
echo '</ul>';
}
我在 ACF 插件中创建了一个字段。我添加了下面的代码以将数据显示为自定义选项卡,但即使该字段为空,该选项卡也始终可见。我错过了什么?
// 1.Displays the tab on every product page
add_filter( 'woocommerce_product_tabs', 'woo_new_tab' );
function woo_new_tab( $tabs ) {
// Adds the new tab
if (!empty(get_the_content())) {
$tabs['application'] = array(
'title' => __( 'Application', 'woocommerce' ),
'priority' => 20,
'callback' => 'woo_new_tab_content'
);
return $tabs;
}
}
// the callback (refer to https://www.advancedcustomfields.com/resources/code-examples/) for more info
function woo_new_tab_content() {
// The new tab content
//Working with Array values (checkbox, select, relationship, repeater) use below
$values = get_field('application'); //field name
if($values){
echo '<ul>';
foreach($values as $value){
echo '<li>' . $value . '</li>';
}
echo '</ul>';
}
}
要在内容为空时隐藏自定义产品选项卡,请改用:
// Add a custom product tab on single product pages
add_filter( 'woocommerce_product_tabs', 'woo_new_tab' );
function woo_new_tab( $tabs ) {
$values = get_field('application');
// Adds the new tab
if ( ! empty($values) ) {
$tabs['application'] = array(
'title' => __( 'Application', 'woocommerce' ),
'priority' => 20,
'callback' => 'woo_new_tab_content'
);
}
return $tabs;
}
// Displays the tab content
function woo_new_tab_content() {
$values = (array) get_field('application');
echo '<ul>';
foreach ( $values as $value ) {
echo '<li>' . $value . '</li>';
}
echo '</ul>';
}