【问题标题】:Exclude categories from WordPress WooCommerce shop从 WordPress WooCommerce 商店中排除类别
【发布时间】:2014-08-01 14:35:37
【问题描述】:

我一直在尝试找出一种方法,从我的WooCommerce 商店首页中排除一个称为特色的特定类别,该页面按显示子类别显示。

我发现这个脚本放置在我的 WordPress 主题的 functions.php 文件中,但似乎什么也没发生:

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( 'featured' ),
        'operator' => 'NOT IN'
    )));

    remove_filter( 'pre_get_posts', 'custom_pre_get_posts_query' );

}

【问题讨论】:

  • 不要只是假设每个人都知道 WooCommerce 是一个 WordPress 插件。在您的帖子中包含此信息。已编辑。
  • 如果我浪费了任何时间,我已经解决了这个问题。
  • 如果您已经解决了问题,能否将其发布为答案,以便其他遇到您问题的人知道您解决了问题?
  • 当然 :) 打勾

标签: php wordpress woocommerce


【解决方案1】:

为了解决我的上述问题 - 我想排除特定类别发布在我的商店页面上的任何位置。

我将它添加到页面 content-product_cat.php 的顶部,就在开头的 <?php 标记下方,并修改了类别名称以反映我需要隐藏的类别。您可以使用逗号分隔多个类别的广告。

if ( is_shop() && in_array( $category->slug, array( 'featured' ) ) ) {
  return;
}

【讨论】:

    【解决方案2】:

    就我而言,我想禁用商店产品列表中的所有类别,包括未来的类别,所以我转到 archive-product.php 文件并评论了 woocommerce_product_subcategories() 功能。

    <?php woocommerce_product_loop_start(); ?>
       <?php //woocommerce_product_subcategories(); //This line of code ?>
       <?php while ( have_posts() ) : the_post(); ?>
          <?php wc_get_template_part( 'content', 'product' ); ?>
       <?php endwhile; // end of the loop. ?>
    <?php woocommerce_product_loop_end(); ?>
    

    这样我就不必每次客户创建新类别的产品时都回来。

    【讨论】:

      【解决方案3】:

      我只是简单地评论了这两行,发现可以正常工作。我不确定评论这些行的后续后果。

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

      【讨论】:

        【解决方案4】:

        您必须将以下代码添加到 functions.php 到您的child theme

        首先你必须找到categories slug,在这里你可以如何做到这一点:

        请记住类别 SLUG 是类别的短标题。

        第 1 步。

        /*get cat slug or cat info*/
             add_action('woocommerce_archive_description', 
        'woocommerce_category_description', 2);
           function woocommerce_category_description() {
              if (is_product_category()) {
              global $wp_query;
            $cat = $wp_query->get_queried_object();
            echo "CAT IS:".print_r($cat,true); // the category needed.
        }}
        

        然后你可以在商店页面输入任何类别时看到类别信息。

        因此,您必须添加此部分以过滤您不想在商店页面中显示的任何特定类别:

        第 2 步。

            /* Exclude Category from Shop*/
        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( 'myCat1', 'myCat2, 'myCat3','myCat4' ) ) ) {
                $new_terms[] = $term;
              }
        
            }
        
            $terms = $new_terms;
          }
        
          return $terms;
        }
        

        记得写你的分类 slug 名称而不是 myCat1...myCat4

        您必须像这样输入类别 slug: if ( ! in_array( $term->slug, array( 'myCat1', 'myCat2', 'myCat3','myCat4')

        祝你好运:)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-12-15
          • 1970-01-01
          • 2018-03-03
          • 1970-01-01
          • 2018-02-12
          • 2015-12-19
          • 2022-12-28
          相关资源
          最近更新 更多