首页 文章

WooCommerce - 在首页显示类别及其产品

提问于
浏览
0

当用户访问我的商店主页时,他看到了商店里的所有商品 . 我想更改视图,以便用户看到:

- Category 1

猫的产品1

- Category 2

猫的产品2

我怎样才能做到这一点?我看了 woocommerce/templates/archive-product.php 并发现了这个:

<?php woocommerce_product_loop_start(); ?>

                <?php woocommerce_product_subcategories(); ?>

                <?php while ( have_posts() ) : the_post(); ?>

                    <?php wc_get_template_part( 'content', 'product' ); ?>

                <?php endwhile; // end of the loop. ?>

            <?php woocommerce_product_loop_end(); ?>

但是我怎么能挂钩,为cat 1和cat 2创建两个不同的循环呢?

有人知道吗?谢谢!

1 回答

  • 1

    首先,您需要查询某个类别的产品:

    $args = array(
        'posts_per_page' => -1,
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => 'category-slug1' //Your category goes here
            ),
        ),
        'post_type' => 'product',
        'orderby' => 'title',
    );
    $first_cat_query = new WP_Query( $args );
    

    然后只需循环打印每个产品:

    while ( $first_cat_query->have_posts() ) {
        $first_cat_query->the_post();
        echo '' . get_the_title() . '

    '; } wp_reset_postdata();

    并重复相同的下一个类别,您需要调整模板以使其与查询兼容 .

相关问题