简体   繁体   English

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

[英]WooCommerce - Display categories and its products on front page

when the user visits my shop homepage he sees all the products which are in the shop. 当用户访问我的商店首页时,他会看到商店中的所有产品。 I want to change the view, so that the user sees: 我想更改视图,以便用户看到:

- Category 1 -1类

Products of cat 1 猫1的产品

- Category 2 -2类

Products of cat 2 猫2的产品

How can I achieve this? 我该如何实现? I looked in woocommerce/templates/archive-product.php and found this: 我查看了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(); ?>

But how can I hook into this, to create two different loops for cat 1 and cat 2? 但是,如何才能为cat 1和cat 2创建两个不同的循环呢?

Does somebody knows that? 有人知道吗? Thanks! 谢谢!

First you'll need to query the products from a category: 首先,您需要从以下类别中查询产品:

$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 );

Then just do a loop to print each product: 然后只需执行循环以打印每个产品:

while ( $first_cat_query->have_posts() ) {
    $first_cat_query->the_post();
    echo '' . get_the_title() . '<br /><br />';
}
wp_reset_postdata();

And repeat the same to the next category, you'll need to adapt the template to make it compatible with the query. 并对下一个类别重复同样的操作,您需要调整模板以使其与查询兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM