【问题标题】:How to group woocommerce products by category in WordPress如何在 WordPress 中按类别对 woocommerce 产品进行分组
【发布时间】:2018-12-10 05:50:23
【问题描述】:

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

WordPress 中的分类如下所示:

  • Lidingö(父类别)

    • Direktåtkomst Fö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();

?>

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

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

【问题讨论】:

  • 你能给你的网站链接吗?预期的输出是什么?

标签: php wordpress woocommerce


【解决方案1】:

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

您需要通过将要搜索的类别添加到参数来缩小搜索范围。例如,如果您想通过类别 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>';
}

【讨论】:

    【解决方案2】:

    首先,您需要将此 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
                    }
                }
            }   
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-21
      • 2020-01-05
      • 1970-01-01
      • 2015-12-09
      • 2022-08-17
      • 1970-01-01
      • 2018-01-17
      • 1970-01-01
      相关资源
      最近更新 更多