programing

woocommerce 제품에 콘텐츠가 없을 때 사용자 지정 탭 숨기기

easyjava 2023. 3. 15. 20:02
반응형

woocommerce 제품에 콘텐츠가 없을 때 사용자 지정 탭 숨기기

필드 상자에 내용이 없는 경우 사용자 정의 탭을 숨길 수 있는 방법이 있습니까?고급 커스텀 필드 플러그인으로 구현하고 있습니다.아직까지는 콘텐츠가 배치되지 않았더라도 탭이 남아 있습니다.

제 함수에 넣은 코드는 다음과 같습니다.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');

}

갱신하다

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

}

헤즈, 나도 같은 문제가 있었는데 이걸 해결할 더 좋은 방법을 찾았어.탭은 내용이 있을 때만 추가합니다.어쩌면 이게 다른 사람들이 이 실마리를 찾는 데 도움이 될지도 몰라

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');

}

더 나은 방법이 있을 수 있지만, 저는 과거에 다음과 같은 방법으로 이를 달성했습니다.

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

텍스트가 있는 경우 "directions" 필드의 내용을 인쇄하고, 없는 경우 css를 인쇄하여 탭을 숨깁니다.

더 좋은 방법은

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

탭에 내용이 없는 경우 탭이 숨겨집니다.

가장 쉬운 방법은 탭을 제거하는 것입니다.

텍스트 필드의 내용을 기준으로 이 작업을 수행할 수 있습니다. 빈 경우 이 작업을 사용하십시오.

 unset( $tabs['direction_tab'] );   // Remove the additional direction tab

이것으로 끝입니다:)

get_field()를 사용하여 사용 가능한 콘텐츠가 있는지 확인할 수 있습니다.

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

// Check if there is content and add the tab if so
if(get_field(direction_tab)){
  $tabs['direction_tab'] = array(
      'title'     => __( 'Direction', 'woocommerce' ),
      'priority'  => 60,
      'callback'  => 'woo_new_direction_tab_content'
  );
}

return $tabs;

}

function woo_new_direction_tab_content() {
    echo get_field('directions');
}

2021년에는 이 코드를 사용할 수 있습니다.원하는 필드가 꽉 찬 경우에만 사용자 지정 탭을 추가합니다.필드가 비어 있으면 탭이 추적 없이 숨겨집니다.

    //Add a custom product data tab

add_filter( 'woocommerce_product_tabs', 'woo_new_custom_tab' );
function woo_new_custom_tab( $tabs ) {
    global $post, $product;
    
    // Adds the new tab

    if( get_field('field_123') ) {
        $tabs['custom_tab'] = array(
            'title'     => __( 'Custom Tab', 'woocommerce' ),
            'priority'  => 25,
            'callback'  => 'woo_new_custom_tab_content'
        );
    }
    
    return $tabs;

}

   //Add content to a tab and hide it if it is empty

function woo_new_custom_tab_content() {
    global $post, $product;
    
    if( get_field('field_123') ) {
        echo '<h2>Custom Tab</h2>';
        echo '<p>'.get_field('field_123',$post->ID).'</p>';
    }
}

기본적으로 탭 내용을 포함하는 필드를 사용하여 탭을 조건부로 표시합니다.코드가 필드가 비어 있는지 확인하고 비어 있으면 탭 설정을 해제하여 표시되지 않습니다.필드에 내용이 있으면 반환됩니다.탭 내용도 조건부로 조정했습니다.즉, 내용이 있는지 확인하고 탭으로 돌아갑니다.체크가 없는 경우에도 필드에 내용이 없는 경우 탭이 반환되지 않으므로 이 옵션은 선택 사항입니다.

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'
    );
    
    if(empty(get_field('directions'))): 
   		
   	unset( $tabs['direction_tab'] );
   	
    else:
    
        return $tabs;
    	
    endif;
}

function woo_new_direction_tab_content() {

    // The new tab content
    if(get_field('directions')): 
        echo '<h2>Directions</h2>';
        the_field('directions'); 
    endif;

}

언급URL : https://stackoverflow.com/questions/26884272/hide-custom-tab-when-no-content-is-present-in-woocommerce-products

반응형