当woocommerce产品中没有内容时隐藏自定义选项卡

cle*_*ruz 9 html php wordpress function woocommerce

如果字段框中没有内容,有没有办法隐藏自定义选项卡。我正在使用高级自定义字段插件来实现这一点。到目前为止,即使没有放置内容,该选项卡仍然存在

这是我放在我的functions.php中的代码

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

// Adds the new tab

    $tabs['direction_tab'] = array(
        'title'     => __( 'Direction', 'woocommerce' ),
        'priority'  => 60,
        'callback'  => 'woo_new_direction_tab_content'
    );

    return $tabs;

}


function woo_new_direction_tab_content() {

    // The new tab content

    echo the_field('directions');

}
Run Code Online (Sandbox Code Playgroud)

更新

//Direction Tab
add_filter( 'woocommerce_product_tabs', 'woo_new_direction_tab' );
function woo_new_direction_tab( $tabs ) {

// Adds the new tab

    $tabs['direction_tab'] = array(
        'title'     => __( 'Direction', 'woocommerce' ),
        'priority'  => 60,
        'callback'  => 'woo_new_direction_tab_content'
    );


    return $tabs;





}


function woo_new_direction_tab_content() {

    if( get_field('directions') )
    {
        echo the_field('directions');
    }

    else
    {
        echo "<style>li.direction_tab_tab{ display:none !important; }</style>";
    }

}
Run Code Online (Sandbox Code Playgroud)

小智 7

Hej,我遇到了同样的问题,并找到了一个更好的方法来解决这个问题。我只在有内容时添加选项卡。也许这有助于其他人找到这个线程。

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

    // Adds the new tab

    if (!empty(the_field('directions'))) {
        $tabs['direction_tab'] = array(
            'title'     => __( 'Direction', 'woocommerce' ),
            'priority'  => 60,
            'callback'  => 'woo_new_direction_tab_content'
        );
    }

    return $tabs;

}


function woo_new_direction_tab_content() {

    // The new tab content

    echo the_field('directions');

}
Run Code Online (Sandbox Code Playgroud)


How*_*wli 4

很可能有更好的方法来做到这一点,但我过去已经通过以下方法实现了这一点:

if( get_field('directions') )
{
    echo the_field('directions');
}
else
{
    echo "<style>.direction_tab_tab { display:none !important; }</style>";
}
Run Code Online (Sandbox Code Playgroud)

如果其中有文本,这将打印“directions”字段的内容,如果没有,它将打印 css 并隐藏选项卡。