如何在WordPress中按类别对woocommerce产品进行分组 [英] How to group woocommerce products by category in WordPress

查看:42
本文介绍了如何在WordPress中按类别对woocommerce产品进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我至少有 4 个父类别,每个父类别都有一个子类别.

I have at least 4 Parent Categories and each parent category has a sub-category.

WordPress 中的类别如下所示:

The categories in WordPress looks like this:

  • Lidingö(父类别)

  • Direktåtkomst Förrådslänga(子类别)
    • 7 kvm(产品)
    • 6 kvm(产品)
    • 1 公里(产品)
    • 1.5 kvm(产品)

    Nacka(父类别)

    • 示例(子类别)
      • aa(产品)
      • bbb(产品)

      我想在 WordPress 中查询产品,它们应该按类别分组.

      I want to query the products in WordPress and they should be grouped by categories.

      这是我当前的代码:

      <?php
      
          $args = array(
              'post_type' => 'product',
              array(
                  'taxonomy' => 'product_cat' 
              ),
              'posts_per_page' => 6,
          );
          $loop = new WP_Query( $args );
          if ( $loop->have_posts() ) {
      
              while ( $loop->have_posts() ) : $loop->the_post();
      
                  echo woocommerce_template_single_title();
      
              endwhile;
          } else {
              echo __( 'No products found' );
          }
          wp_reset_postdata();
      
      ?>
      

      我认为上面的代码不正确,因为每个分页都显示相同的产品.

      I think the code above is not correct because the same products are being displayed for every pagination.

      您知道按类别对所有 woocommerce 产品进行分组的正确查询是什么吗?谢谢

      Do you have any idea what is the correct query to group all woocommerce products by category? Thanks

      推荐答案

      您的查询需要属于产品类别分类法的所有产品 - 即所有产品.

      Your query is asking for all products that belong to the product category taxonomy - that is, all of them.

      您需要通过将要搜索的类别添加到参数来缩小搜索范围.例如,如果您想通过类别 slug 进行搜索,您可以使用:

      You need to narrow the search by adding the category you wish to search to the arguments. For example, if you wanted to search via the category slug, you would use:

      $args = array(
          'post_type' => 'product',
          array(
              'taxonomy' => 'product_cat',
              'field' => 'slug',
              'terms' => 'category-slug1'
          ),
          'posts_per_page' => 6,
      );
      

      要遍历一页上的所有类别,您需要这样的内容:

      To loop through all the categories on one page, you'll want something like this:

      $my_categories = get_terms( 'TERM_NAME_HERE' );
      $my_categories_count = count( $my_categories );
      
      if ( $my_categories_count > 0 && is_array( $my_categories ) ) {
          echo '<div class="wrap">';
          foreach ( $my_categories as $single_cat ) { ?>
              <h2><?php echo $single_cat->name; ?></h2>
      
              <?php
                  $cat_posts_args = array(
                      'post_type' => 'product',
                      'order' => 'ASC',
                      'orderby' => 'date',
                      'post_status' => 'publish',
                      'posts_per_page' => -1,
                      'tax_query' => array(
                          array(
                              'taxonomy' => 'product_cat',
                              'field' => 'id',
                              'terms' => $single_cat->term_id,
                              'include_children' => false
                          )
                      )
                  );
                  $cat_posts = new WP_Query( $cat_posts_args );
                  if ( $cat_posts->have_posts() ) :
                      echo '<p>';
                      while ( $cat_posts->have_posts() ) : $cat_posts->the_post(); ?>
      
                          <a href="<?php the_permalink(); ?>"><span><?php the_title(); ?></span>: <?php echo get_the_excerpt(); ?></a><br>
      
                      <?php endwhile;
                      echo '</p>';
                  else :
                      if ( !$parent ) echo '<p>No products found.</p>';
                  endif;
                  wp_reset_postdata();
              } // end foreach
          echo '</div>';
      }
      

      这篇关于如何在WordPress中按类别对woocommerce产品进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆