【问题标题】:Hide category in the WooCommerce shop page在 WooCommerce 商店页面中隐藏类别
【发布时间】:2012-11-02 05:41:44
【问题描述】:

我一直在尝试从 SHOP 页面隐藏特定类别。我找到了这段代码:

add_filter( 'pre_get_posts', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

    if ( ! $q->is_main_query() ) return;
    if ( ! $q->is_post_type_archive() ) return;

    $q->set( 'tax_query', array(array(
        'taxonomy' => 'product_cat',
        'field' => 'slug',
        'terms' => array( 'CATEGORY TO HIDE' ),
        'operator' => 'NOT IN'
    )));

    remove_filter( 'pre_get_posts', 'custom_pre_get_posts_query' );

}

我已将此代码粘贴到我的主题 function.php 文件中,但我没有实现结果...

有人可以帮帮我吗?

【问题讨论】:

  • 请修正您帖子的标题。所有的大写都是粗鲁的......

标签: php woocommerce


【解决方案1】:

我知道这有点晚了,但我自己也遇到了这个问题,并用以下函数解决了这个问题:

add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );

function get_subcategory_terms( $terms, $taxonomies, $args ) {

  $new_terms = array();

  // if a product category and on the shop page
  if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) {

    foreach ( $terms as $key => $term ) {

      if ( ! in_array( $term->slug, array( '**CATEGORY-HERE**' ) ) ) {
        $new_terms[] = $term;
      }

    }

    $terms = $new_terms;
  }

  return $terms;
}

【讨论】:

  • 我复制粘贴了代码。添加了类别slug。删除is_shop()。但是woocommerce的单品页面上仍然显示类别名称。
  • 为什么要删除 is_shop() ?
  • 我只是不希望它出现在网站的任何地方。不仅在商店页面。这就是为什么。
  • 你有没有想过这个问题?我遇到了同样的问题,并用 is_page() 而不是 is_shop() 尝试了上面的代码,但仍然没有运气。
【解决方案2】:

下面的 sn-p 对我来说很好用:

add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

    if ( ! $q->is_main_query() ) return;
    if ( ! $q->is_post_type_archive() ) return;

    if ( ! is_admin() && is_shop() ) {

        $q->set( 'tax_query', array(array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => array( 'your category slug' ), // Don't display products in the knives category on the shop page
            'operator' => 'NOT IN'
        )));

    }

    remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

}

我想知道如何在类别中排除的产品通过产品搜索实现相同的搜索,同时使用 sn-p 这些产品完全隐藏。

【讨论】:

    【解决方案3】:

    对除管理后端之外的所有内容隐藏类别的简单方法:

    functions.php:

    add_filter( 'get_terms', 'hide_category', 10, 1 );
    function hide_category( $terms ) {
      $new_terms = array();
      foreach ( $terms as $term ) {
        if ( $term->slug !== 'secret_category' ) {
          $new_terms[] = $term;
        } else if ( $term->taxonomy !== 'product_cat' || is_admin() ) {
          $new_terms[] = $term;
        }
      }
      return $new_terms;
    }
    

    如果您只想对商店隐藏它,请将|| !is_shop() 添加到else if 条件中。

    【讨论】:

      【解决方案4】:

      如果您想在主题中隐藏某些类别,您只需在 wp_list_categories 函数中传递 exclude 参数即可:

      wp_list_categories( array(
      'taxonomy'              =>  'product_cat',
      'hide_empty'            =>  1,
      'use_desc_for_title'    =>  0,
      'title_li'              =>  ' ',
      'show_count'            =>  0,
      'exclude'               =>  '63'    // <-- Hidden) );
      

      【讨论】:

        猜你喜欢
        • 2019-07-06
        • 1970-01-01
        • 2018-03-03
        • 1970-01-01
        • 1970-01-01
        • 2018-09-01
        • 1970-01-01
        • 2015-12-19
        • 2019-09-17
        相关资源
        最近更新 更多