【问题标题】:Hide custom tab when no content is present in woocommerce products当 woocommerce 产品中没有内容时隐藏自定义选项卡
【发布时间】:2015-01-09 03:29:55
【问题描述】:

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

这是我放在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');

}

更新

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

}

【问题讨论】:

    标签: php html wordpress function woocommerce


    【解决方案1】:

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

    【讨论】:

    • 2021年不行,你得加全局$post;然后调用 get_field('directions',$post->ID)。只是 the_field('directions') 是一个输出函数,你不能用 if 子句检查
    【解决方案2】:

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

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

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

    【讨论】:

    • 美好的一天,我已经根据您提供的解决方案更新了我的 functions.php 中的代码,但它仍然在界面上显示了方向选项卡:(
    • @clestcruz 您可能需要查看正在调用该选项卡的类。我猜测它被称为.direction_tab_tab,对你来说可能会有所不同。
    • 不,您提到的课程完全相同。我什至放了 li.direction_tab_tab
    • &lt;style&gt;.direction_tab_tab { display:none !important; }&lt;/style&gt; 是否被打印出来了?
    • 我检查了我的内联样式,它似乎没有呼应样式
    【解决方案3】:

    更好的方法是

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

    如果选项卡中没有内容,这将隐藏选项卡

    【讨论】:

      【解决方案4】:

      最简单的方法是删除标签。

      你可以根据文本字段的内容来做,如果是空的就用这个。

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

      你完成了:)

      【讨论】:

      • 美好的一天,我正在寻找的正是让标签在有内容时出现。但如果不存在任何内容,则不会出现该选项卡。我不想永久删除标签
      【解决方案5】:

      您可以使用 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');
      }
      

      【讨论】:

        【解决方案6】:

        此代码在 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>';
            }
        }
        

        【讨论】:

          【解决方案7】:

          基本上,您使用包含选项卡内容的字段来有条件地显示选项卡。代码检查该字段是否为空,如果是,则取消设置选项卡,使其不显示。如果该字段有内容,则返回。我也用条件调整了标签内容。 IE。检查是否有内容,如果有则返回选项卡。这是可选的,因为即使没有检查,如果字段中没有内容,也不会返回选项卡。

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

          【讨论】:

          • 基本上你使用带有标签内容的字段来有条件地显示标签。代码检查该字段是否为空,如果是,则取消设置选项卡,使其不显示。如果该字段有内容,则返回。我也用条件调整了标签内容。 IE。检查是否有内容,如果有则返回选项卡。这是可选的,因为即使没有检查,如果字段中没有内容,也不会返回选项卡。
          猜你喜欢
          • 2021-07-24
          • 2021-07-12
          • 1970-01-01
          • 2020-02-18
          • 1970-01-01
          • 1970-01-01
          • 2018-02-26
          • 2016-06-01
          • 1970-01-01
          相关资源
          最近更新 更多