Php 内容为空时如何隐藏自定义选项卡

Php 内容为空时如何隐藏自定义选项卡,php,wordpress,woocommerce,e-commerce,Php,Wordpress,Woocommerce,E Commerce,我只是在woocommerce单一产品页面中添加了4个自定义选项卡。现在,如果没有内容,我想隐藏这些选项卡。这是我的自定义标签代码。我该怎么做?如果没有内容,则不显示“自动说明”选项卡。另一个问题是查看选项卡。如果没有评论或评论,我也可以隐藏它吗? 提前谢谢 // Add a custom metabox add_action( 'add_meta_boxes', 'additional_product_tabs_metabox' ); function additional_product_

我只是在woocommerce单一产品页面中添加了4个自定义选项卡。现在,如果没有内容,我想隐藏这些选项卡。这是我的自定义标签代码。我该怎么做?如果没有内容,则不显示“自动说明”选项卡。另一个问题是查看选项卡。如果没有评论或评论,我也可以隐藏它吗? 提前谢谢

// Add a custom metabox

add_action( 'add_meta_boxes', 'additional_product_tabs_metabox' );
function additional_product_tabs_metabox()
{
add_meta_box(
    'add_product_metabox_additional_tabs',
    __( 'Additional product Tabs', 'woocommerce' ),
    'additional_product_tabs_metabox_content',
    'product',
    'normal',
    'high'
);
}

//  Add custom metabox content
function additional_product_tabs_metabox_content( $post )
{
// Indicated For
echo '<h4>' . __( 'Indicated for', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_indicated_for', true );
wp_editor( $value, '_indicated_for', array( 'editor_height' => 100 ) );

// Caution
echo '<br><hr><h4>' . __( 'Caution', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_caution', true );
wp_editor( $value, '_caution', array( 'editor_height' => 100 ) );


// How To
echo '<br><hr><h4>' . __( 'How To', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_how_to', true );
wp_editor( $value, '_how_to', array( 'editor_height' => 100 ) );

//Part of Cure Series

echo '<br><hr><h4>' . __( 'Part of Cure Series', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_part_of_cure_series', true );
wp_editor( $value, '_part_of_cure_series', array( 'editor_height' => 100 ) );

// Nonce field (for security)
echo '<input type="hidden" name="additional_product_tabs_nonce" value="' . wp_create_nonce() . '">';
}


// Save product data
add_action( 'save_post_product', 'save_additional_product_tabs', 10, 1 );
function save_additional_product_tabs( $post_id ) {

// Security check
if ( ! isset( $_POST[ 'additional_product_tabs_nonce' ] ) ) {
    return $post_id;
}

//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST[ 'additional_product_tabs_nonce' ] ) ) {
    return $post_id;
}

// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    return $post_id;
}

if ( ! current_user_can( 'edit_product', $post_id ) ) {
    return $post_id;
}

// Sanitize user input and save the post meta fields values.

if( isset($_POST[ '_indicated_for' ]) )
    update_post_meta( $post_id, '_indicated_for', wp_kses_post($_POST[ '_indicated_for' ]) );

if( isset($_POST[ '_caution' ]) )
    update_post_meta( $post_id, '_caution', wp_kses_post($_POST[ '_caution' ]) );

if( isset($_POST[ '_how_to' ]) )
    update_post_meta( $post_id, '_how_to', wp_kses_post($_POST[ '_how_to' ]) );

if( isset($_POST[ '_part_of_cure_series' ]) )
    update_post_meta( $post_id, '_part_of_cure_series', wp_kses_post($_POST[ '_part_of_cure_series' ]) );

}

add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
function woo_custom_product_tabs( $tabs ) {

// 1) Removing tabs

//unset( $tabs['description'] );              // Remove the description tab
//unset( $tabs['additional_information'] );   // Remove the additional information tab



//Attribute Indicated for
$tabs['intend_tab'] = array(
    'title'     => __( 'Intend For', 'woocommerce' ),
    'priority'  => 50,
    'callback'  => 'woo_indicated_for_tab_content'
);

// Adds the Caution tab
$tabs['caution_tab'] = array(
    'title'     => __( 'Caution', 'woocommerce' ),
    'priority'  => 60,
    'callback'  => 'woo_caution_tab_content'
);

// Adds the how to tab
$tabs['how_to'] = array(
    'title'     => __( 'How To', 'woocommerce' ),
    'priority'  => 70,
    'callback'  => 'woo_how_to_content'
);

 // Adds the _part_of_cure_series

$tabs['part_of_cure_series'] = array(
    'title'     => __( 'Part of Cure Series', 'woocommerce' ),
    'priority'  => 80,
    'callback'  => 'woo_part_of_cure_series'
);



$tabs['reviews']['priority'] = 90;

return $tabs;
}


function woo_indicated_for_tab_content()  {
global $product;


echo'<div><p>'. $product->get_meta( '_indicated_for' ) . '</p></div>';
}

function woo_caution_tab_content()  {
global $product;

echo'<div><p>'. $product->get_meta( '_caution' ) . '</p></div>';
}

function woo_how_to_content()  {
global $product;

echo'<div><p>'. $product->get_meta( '_how_to' ) . '</p></div>';
}

function woo_part_of_cure_series()  {
global $product;

echo'<div><p>'. $product->get_meta( '_part_of_cure_series' ) . '</p></div>';
}
//添加自定义元盒
添加动作(“添加元框”、“附加产品”选项卡“元框”);
功能附加\u产品\u选项卡\u元盒()
{
添加元框(
“添加产品、元框和其他选项卡”,
__(“其他产品选项卡”、“woocommerce”),
“附加产品选项卡元框内容”,
“产品”,
“正常”,
“高”
);
}
//添加自定义元框内容
功能附加\产品\选项卡\元数据库\内容($post)
{
//用于
回显“”。uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
$value=get_post_meta($post->ID,'.'表示'u for',true);
wp_编辑器($value,''.'表示''.'的''.'数组('editor_高度'=>100));
//谨慎
回音“

”。u uu('Caution'、'woocommerce'); $value=get_post_meta($post->ID,'u caution',true); wp_编辑器($value,''u caution',数组('editor_height'=>100)); //如何 回音“

”。u uu('How To','woocommerce'); $value=get_post_meta($post->ID,'u how_to',true); wp_编辑器($value,''u how_to',数组('editor_height'=>100)); //Cure系列的一部分 回音“

”。u uuu(“治愈系列的一部分”,“woocommerce”); $value=get_post_meta($post->ID,“_cure_系列的一部分”,true); wp_编辑器($value,“_-cure_系列的第u部分”,数组('editor_-height'=>100)); //Nonce字段(用于安全) 回声'; } //保存产品数据 添加操作('save_post_product'、'save_additional_product_tabs',10,1); 功能保存\其他\产品\选项卡($post\ id){ //安全检查 如果(!isset($\u POST['其他产品标签]])){ 返回$post_id; } //验证nonce是否有效。 如果(!wp\u verify\u nonce($\u POST['其他产品标签\u nonce')){ 返回$post_id; } //如果这是自动保存,则我们的表单尚未提交,因此我们不想执行任何操作。 if(已定义('DOING_AUTOSAVE')&&DOING_AUTOSAVE){ 返回$post_id; } 如果(!当前用户可以('编辑产品',$post\U id)){ 返回$post_id; } //清理用户输入并保存post元字段值。 如果(isset($_POST['.\u表示'.]])) 更新发布元数据($发布id,'''''''''''''''.'表示''.'的'.'发布'.''); 如果(isset($_POST['注意])) 更新发布元数据($发布id,'''''''''''''''''''''''''''''''''''''''''.'警告'',wp'''u kses'.'发布($'''''''.'发布['''''.'警告']); 如果(isset($\u POST['\u how\u to'])) 更新发布元数据($发布id,'''''''''''''''''''''''''.'如何''.'到''.'如何''.'到'.''); 如果(isset($_POST['_cure_series'的部分]) 更新发布元($发布id,$发布系列的第部分,$发布[$发布系列的第部分]); } 添加过滤器(“woocommerce\u product\u tabs”、“woo\u custom\u product\u tabs”); 功能自定义产品选项卡($tabs){ //1)卸下卡舌 //取消设置($tabs['description']);//删除描述选项卡 //取消设置($tabs['additional_information']);//删除附加信息选项卡 //为指定的属性 $tabs['intential_tab']=数组( “title”=>uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu, “优先级”=>50, “callback”=>“woo\u表示\u选项卡\u内容” ); //添加“警告”选项卡 $tabs['caution_tab']=数组( “title”=>uuuuu(‘小心’、‘小心’), “优先级”=>60, “回调”=>“警告选项卡内容” ); //添加“如何”选项卡 $tabs['how_to']=数组( 'title'=>uuu('How To','woocommerce'), “优先级”=>70, “回调”=>“woo_how_to_content” ); //添加“治愈”系列的“部分” $tabs['part_of_cure_series']=数组( 'title'=>uuuuu('Cure系列的一部分,'woocommerce'), “优先级”=>80, “回调”=>“woo_系列的第部分” ); $tabs['reviews']['priority']=90; 返回$tabs; } 函数woo_表示_选项卡_内容(){ 全球$产品; 回显“”。$product->get_meta(“_表示“)”。

”; } 函数woo_小心_选项卡_内容(){ 全球$产品; 回显“”。$product->get_meta(“'u caution')。

”; } 函数woo_how_to_content(){ 全球$产品; 回显“”。$product->get_meta(“'u how_to')。

”; } 函数woo__系列()的第_部分{ 全球$产品; 回显“”。$product->get_meta(“\u cure_系列的部分”)。

”; }
在活动主题的functions.php中添加以下代码片段以实现上述功能-

function modify_woo_custom_product_tabs( $tabs ) {
    global $product;
    // Remove tabs if contents data are empty
    if( isset($tabs['intend_tab'] ) && !$product->get_meta( '_indicated_for' ) ) {
        unset( $tabs['intend_tab'] ); 
    }
    if( isset($tabs['caution_tab'] ) && !$product->get_meta( '_caution' ) ) {
        unset( $tabs['caution_tab'] ); 
    }
    if( isset($tabs['how_to'] ) && !$product->get_meta( '_how_to' ) ) {
        unset( $tabs['how_to'] ); 
    }
    if( isset($tabs['part_of_cure_series'] ) && !$product->get_meta( '_part_of_cure_series' ) ) {
        unset( $tabs['part_of_cure_series'] ); 
    }
    if( isset($tabs['reviews'] ) && $product->get_review_count() ) {
        unset( $tabs['reviews'] ); // unset reviews
    }
    return $tabs;

}
add_filter( 'woocommerce_product_tabs', 'modify_woo_custom_product_tabs', 999 );

我知道这是非常晚的回答,但这是一般性的,可以为人有用

    /**
     * Remove product data tabs when the content is empty
     */
    function demo_remove_product_tabs( $tabs ) {
        foreach ( $tabs as $key=>$product_tab) {
            if ( isset( $product_tab['callback'] ) ) {
                ob_start();
                call_user_func( $product_tab['callback'], $key, $product_tab );
                $tab_content = ob_get_clean();
                if (empty($tab_content)){
                    unset($tabs[$key]); 
                }
            }
        }
        return $tabs;
    }
    add_filter( 'woocommerce_product_tabs', 'demo_remove_product_tabs', 98 );