【问题标题】:Hide custom product tab if content is empty in WooCommerce single products如果 WooCommerce 单品中的内容为空,则隐藏自定义产品选项卡
【发布时间】:2021-07-12 20:18:15
【问题描述】:

我在 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>';
    }
}

【问题讨论】:

    标签: php wordpress woocommerce product advanced-custom-fields


    【解决方案1】:

    要在内容为空时隐藏自定义产品选项卡,请改用:

    // 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>';
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-24
      • 2015-01-09
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      • 2020-02-18
      • 2016-06-01
      相关资源
      最近更新 更多