【问题标题】:Remove Woocommerce review tab if empty如果为空,请删除 Woocommerce 评论选项卡
【发布时间】:2016-01-30 17:01:50
【问题描述】:

如何隐藏仅没有评论的产品的评论标签?我找到了这段代码:

add_filter( 'woocommerce_product_tabs', 'delete_tab', 98 ); function delete_tab( $tabs ) { unset($tabs['reviews']); return $tabs; }

但它会删除所有地方的评论,即使在有评论的产品中也是如此。

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    检查这个:

    add_filter( 'woocommerce_product_tabs', 'delete_tab', 98 );
    function delete_tab( $tabs ) {
    
        global $product;
        $id = $product->id;
    
        $args = array ('post_type' => 'product', 'post_id' => $id);    
        $comments = get_comments( $args );
    
        if(empty($comments)) {
            unset( $tabs['reviews'] );
        }
    
        return $tabs;
    }
    

    【讨论】:

    • 感谢分享它工作正常。但你知道如何只显示表格和评级明星吗?主要是我想隐藏 评论 (0) 还没有评论。本文。如果为空
    【解决方案2】:

    最简单的方法是向woocommerce_product_tabs 添加过滤器。在其中,您可以使用global $product,它是对当前产品的引用。该对象有一个名为get_review_count的方法:

    add_filter('woocommerce_product_tabs', function ($tabs) {
        global $product;
    
        if ($product && $product->get_review_count() === 0) {
            unset($tabs['reviews']);
        }
    
        return $tabs;
    }, 98);
    

    【讨论】:

      【解决方案3】:

      这里有一个类似于 Dimitar 的选项的方法,但略短一些:

      add_filter( 'woocommerce_product_tabs', 'delete_tab', 98 );
      
      function delete_tab( $tabs ) {
      
          if ( ! have_comments() ) {
              unset( $tabs['reviews'] );
          }
      
          return $tabs;
      }
      

      【讨论】:

        【解决方案4】:

        是否将其他“标签”放到底部,例如店面?

        【讨论】:

          【解决方案5】:

          删除 Woocommerce 描述标签(如果为空)

          add_filter( 'woocommerce_product_tabs', 'delete_description_tab', 98 );
          function delete_description_tab( $tabs ) {
          
              global $product;
              $id = $product->id;
          
              $data = array ('post_type' => 'product', 'post_id' => $id);    
              $description = get_comments( $data );
          
              if(empty($description)) {
                  unset( $tabs['description'] );
              }
          
              return $tabs;
          }
          

          【讨论】:

            【解决方案6】:

            你也可以试试

            function ps_disable_reviews() {
                    remove_post_type_support( 'product', 'comments' );
                }
                
            add_action( 'init', 'ps_disable_reviews' );
            

            【讨论】:

              猜你喜欢
              • 2020-07-12
              • 2016-02-02
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-07-11
              • 2018-05-21
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多