Woocommerce products from category loop

Aurimas Gelžinis

In Woocommerce, I Need some help with this custom product loop, in my code my result is : how it looks like

The loop doesn't stop and it is looping the same products for three or four times.

The code I am using is here :

<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>
Morten Bak

Your code should work, although it will take all products in the loop (including those marked as "draft" when you are logged in). You can define only published by adding 'post_status' => 'publish' to your argument variable.

And you should take a look at those anchor tags (specifically the close part)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Exclude a product category from Woocommerce related products

Exclude a product category from Related products in WooCommerce

Display products from specific product category in Woocommerce

Exclude Products Category from loops shortcode Woocommerce

Exclude Woocommerce products in recently viewed products widget from a product category

Hide a specific Woocommerce products from loop if they are in cart

Redirect when buying products from a certain product category in Woocommerce

Get all WooCommerce products from a category that have specific meta data

WooCommerce: Only allow products from the same category in cart

Woocommerce how to display products from specific category on archive page

Sort Woocommerce Products from a product category and custom meta_key

Replace Upsells with products from a specific product category in Woocommerce 3

How to get all products from current WooCommerce product category?

remove products of a category from the total count cart woocommerce

Remove woocommerce products only from certain category parent ID

Remove "No products found" from a specific product category in Woocommerce

Set custom quantity arguments to products from specific category in WooCommerce

how to remove a category from woocommerce products through instantsearch js

Only allow add to cart from products of the same parent category in WooCommerce

Products dropdown from same category inside Woocommerce product short description

Exclude a product category from the loop in Woocommerce

WooCommerce: assign a category to all products

Display All Products by Category with WooCommerce

how to show a category of woocommerce products

woocommerce shortcode for products in brand and category

shortcode to show products by category woocommerce

Woocommerce sub category loop

Show product-category thumbnails in loop which have n price of products in woocommerce

Remove Woocommerce free products from a WP_Query loop