当前位置: 首页 > 知识库问答 >
问题:

如何在WordPress中按类别分组WooCommerce产品

班凌
2023-03-14

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

WordPress中的类别如下所示:

>

  • 利丁ö(父类别)

    • Direktåtkomst Förrådslänga(子类别)
    • 1kbm(产品)
    • 1.5千伏安(产品)

    Nacka(父类别)

    • 样本(子类别)
      • aa(产品)
      • bbb(产品)

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

      这是我当前的代码:

      <?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();
      
      ?>
      

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

      你知道什么是正确的查询分组所有的WooCommerce产品按类别?谢啦

  • 共有2个答案

    戚哲
    2023-03-14

    您的查询要求所有属于产品类别分类法的产品,即所有产品。

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

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

    要在一个页面上循环浏览所有类别,您需要这样的东西:

    $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>';
    }
    
    樊琦
    2023-03-14

    首先,您需要将此woocommerce模板覆盖到当前主题,因为您必须使用自定义循环按类别获取产品。

    只要把这个模板复制到你的current_theme-

    使用以下代码:

    $parent_terms= get_terms( array( 'taxonomy' => 'product_cat', 'parent' => 0 ) );
    if($parent_terms= ) 
    {
        foreach( $parent_terms  as $parent_term  ) 
        {
            $child_terms = get_terms( array( 'taxonomy' => 'product_cat', 'parent' => $parent_term->term_id  ) );
            if($child_terms) 
            {
                foreach( $child_terms as $child_term ) 
                {
                    $product_args=
                        array(
                            'posts_per_page' => 50, 
                            'post_type' => 'my_custom_type',
                            'posts_per_page' => 6,
                            'cat' => $child_term->term_id 
                        );
                    $product_query = new WP_Query( $product_args );
                    while($product_query->have_posts()) : $product_query->the_post(); 
                    {
                        Here you will get product detail so you can display product details as you wanted
                    }
                }
            }   
        }
    }
    
     类似资料:
    • 实际上我对WordPress很陌生。我正在使用一个Woocommerce插件来处理产品类别。有没有人知道如何对齐产品,使其在中心,并将文本定位在一个横向的方向?同样,对于我们主页的主题,如果我们使用手机,它倾向于定位在左边而不是中心。我有很少或没有编码背景。有什么css或任何代码我可以使用吗?会很感激任何帮助的。谢谢. 以下是网址:http://aom.sg/product-category/ai

    • 我试图通过WoodPress主题中的一个函数从WooCommerce获取产品类别 上面的代码返回一些产品,但是产品类别。 当我包括

    • 我正在尝试获取子类别的子类别的产品类别 这段代码按层次列出了它们下面的所有顶级类别和子类别,但我有子类别(sub-sub-category)的子类别,因此如何列出这些子类别(使用clic)。 BO中的类别 在FO中列出类别

    • 我正在尝试创建一个页面与木商产品类别在标签。 我的选项卡菜单正在工作,但我需要在每个选项卡内容区域中运行一个查询到相应的类别。 但当我单击每个选项卡时,选项卡内容显示了类别中不属于当前选项卡的所有帖子。我没有得到正在出现的问题,请帮助我解决问题 下面是我的代码: 问题是它显示每个类别的所有post。我粘在上面了..请帮帮我

    • 我第一次使用woocommerce(wordpress)。 我试图在树形中显示所有产品类别及其各自的子类别,但我似乎不能像在后端使用拖放方式订购它们那样订购它们。 WP get_terms()只允许按id、名称、计数、slug和none排序。 我使用的代码是:

    • 我有一个名为“产品类型”的WooCommerce产品类别,我试图列出该类别下的所有类别,但不是这些类别的子类别。例如,我有: 产品类型 碳化物米尔斯 钓鱼工具 儿童类别 我希望它列出“电石磨坊”和“打捞工具”,但不是“儿童类别”。 这是我的代码: 但它仍然返回“儿童类别”。我不知道为什么将深度限制为“1”并将“include_children”设置为“false”不能解决这个问题。