16

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?

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.

1
  • can u please tell me where to add this code if you please Commented Jan 20, 2017 at 19:49

3 Answers 3

29

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-->
8
  • 1
    @VanTong Then mark this answer as the answer: click the checkmark on the left side.
    – fuxia
    Commented Oct 7, 2012 at 4:37
  • Hi Dwaser,I want ask more. If in archive page, I want show all new product. and When I choose specific Category It will show product of that category. Example I have 2 category Category A and Category B,When I choose A, it will show product of A, and choose B it will show product of B. Please tell me loops. thanks.
    – Van Tong
    Commented Oct 26, 2012 at 12:22
  • hot show message if there is no products? Commented Feb 5, 2014 at 23:42
  • @dwaser Hi, deos the product_cat argument accept an int category id, ie: 40 instead of 'shoes' ? if not, how to make it accept an int category id ? thanks
    – Malloc
    Commented May 6, 2014 at 18:15
  • 1
    exicuse me guys, but where to add this code ? i'm a newpie in wordpress but i have a very close deadline. @VanTong Commented Jan 20, 2017 at 19:21
0

Try this

<?php
    echo $product->get_categories(
        ', ',
        '<span class="posted_in">' . _n( 'Category:', 'Categories:',
        sizeof( get_the_terms( $post->ID, 'product_cat' ) ),
        'woocommerce' ) . ' ',
        '.</span>'
    );
?>
1
  • 4
    Can you elaborate on how and why this would solve the OP's problem? Commented Sep 17, 2013 at 8:38
0

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.

2
  • question was for products of a specific category?
    – Jon
    Commented Oct 20, 2020 at 12:44
  • Please delete this answer, as it has nothing to do with the question.
    – clayRay
    Commented Feb 12, 2021 at 3:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.