How to Display Product specific to a category with WooCommerce Plugin?

I’m using plugin Woocommerce to do shop online.

I have many categories for example shoes, clothes and so on. How can I display the products of specific category?

Read More

I see such product loops of on the sample page, but I only want show the products of a specific category in my index.php.

Related posts

Leave a Reply

3 comments

  1. You need to create a new loop for that. Here’s the code I use for displaying products from a specific category on the home page:

    <ul class="products">
        <?php
            $args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'shoes', 'orderby' => 'rand' );
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
    
                <h2>Shoes</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>
    
        <?php endwhile; ?>
        <?php wp_reset_query(); ?>
    </ul><!--/.products-->
    
  2. Try this

    <?php
        echo $product->get_categories(
            ', ',
            '<span class="posted_in">' . _n( 'Category:', 'Categories:',
            sizeof( get_the_terms( $post->ID, 'product_cat' ) ),
            'woocommerce' ) . ' ',
            '.</span>'
        );
    ?>
    
  3. There is another way:

    You can assign the “Shop” page as default home page. Now all the products will displayed to the home page. I mean index.php.