如何在WordPress中按类别对woocommerce产品进行分组

时间:2018-07-02 03:20:34

标签: php wordpress woocommerce

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

WordPress中的类别如下:

  • Lidingö(父类别)

    • DirektåtkomstFörrådslänga(子类别)
      • 7 kvm(产品)
      • 6 kvm(产品)
    • Entréplan(子类别)
      • 1 kbm(产品)
      • 1.5 kvm(产品)
  • 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();

?>

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

enter image description here

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

2 个答案:

答案 0 :(得分:0)

您的查询将询问属于产品类别分类的所有产品,即所有产品。

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

$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>';
}

答案 1 :(得分:0)

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

只需将woocommerce的模板复制到您的current_theme->创建文件夹名称(Woocommerce)->将模板粘贴到该文件夹​​即可。

使用以下代码:

$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
                }
            }
        }   
    }
}