来自类别循环的 Woocommerce 产品

Woocommerce products from category loop

在 Woocommerce 中,我需要一些有关此自定义产品循环的帮助,在我的代码中我的结果是:how it looks like

循环没有停止,它循环了三四次相同的产品。

我使用的代码在这里:

<div class="container">
    <div id="default_products_page_container" class="wrap wpsc_container">
        <?php
            remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
            remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
            $cat = get_query_var( 'product_cat' );
            $args = array( 'post_type' => 'product', 'posts_per_page' => 5, 'product_cat' => $cat, 'orderby' => 'rand' );
            $loop = new WP_Query( $args );
        ?>
        <div class="wpsc_default_product_list">
            <?php
            while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
            <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12">
                <div class="default_product_display product_view_<?php echo get_permalink( $loop->post->ID ); ?>group">
                    <a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>"/>
                    <?php if (has_post_thumbnail( $loop->post->ID ))
                    {            
                    ?>         
                    <div class="product-image-thumb"> 
                        <img src="<?php echo the_post_thumbnail_url( $loop->post->ID );?>"/>
                    </div>
                    <?php
                    } else 
                    {?>
                    <div class="product-image-thumb">
                        <a>
                            <img src="<?php echo woocommerce_placeholder_img_src();?>"/>
                        </a>
                    </div>
                    <?php } ?>
                </div>
            </div>
            <?php endwhile; ?>
        </div>
        <?php wp_reset_query(); ?>
    </div>
</div>

您的代码应该可以工作,尽管它会在循环中使用所有产品(包括您登录时标记为 "draft" 的产品)。您可以通过将 'post_status' => 'publish' 添加到参数变量来定义仅发布。

你应该看看那些锚标签(特别是关闭部分)

您需要这样 tax query 来代替:

$loop = new WP_Query( array(
    'post_type' => 'product',
    'post_status' => 'publish',
    'posts_per_page' => 5,
    'tax_query' => array( array(
        'taxonomy'         => 'product_cat',
        'field'            => 'slug', // Or 'term_id' or 'name'
        'terms'            => get_query_var( 'product_cat' ), // A slug term
        // 'include_children' => false // or true (optional)
    )),
    'orderby' => 'rand'
) );

在 Woocommerce 产品类别存档页面上进行了测试和工作…

我希望这段代码对你们有所帮助:)

    <ul class="products">
    <?php
    $args = array(
        'product_cat' => 'Shampoo',
        'posts_per_page' => 4,
        'orderby' => 'rand'
    );
    $loop = new WP_Query($args);
    while ($loop->have_posts()) : $loop->the_post();
        global $product; ?>
        <div class="row">
            <!-- <h2>Shampoo</h2> -->
            <li class="product">

                <a href="<?php echo get_permalink($loop->post->ID) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">

                    <?php woocommerce_show_product_sale_flash($post, $product); ?>

                    <?php if (has_post_thumbnail($loop->post->ID)) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
                    else echo '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" width="300px" height="300px" />'; ?>

                    <h3><?php the_title(); ?></h3>

                    <span class="price"><?php echo $product->get_price_html(); ?></span>

                </a>

                <?php woocommerce_template_loop_add_to_cart($loop->post, $product); ?>
            </li>
        </div>
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>
</ul>
<!--/.products-->