Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

4 Answers 4

up vote 9 down vote accepted

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-->
share|improve this answer
    
thanks Dwaser much. I've done. that code has worked :D –  Van Tong Oct 5 '12 at 16:13
    
@VanTong Then mark this answer as the answer: click the checkmark on the left side. –  toscho Oct 7 '12 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 Oct 26 '12 at 12:22
    
hot show message if there is no products? –  Muhammad Bilal Feb 5 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 May 6 at 18:15

Try this

<?php
    echo $product->get_categories(
        ', ',
        '<span class="posted_in">' . _n( 'Category:', 'Categories:',
        sizeof( get_the_terms( $post->ID, 'product_cat' ) ),
        'woocommerce' ) . ' ',
        '.</span>'
    );
?>
share|improve this answer
1  
Can you elaborate on how and why this would solve the OP's problem? –  Johannes Pille Sep 17 '13 at 8:38
    
This is a handy piece of code, if you just want to show the categories of a product. But for one, this is a 100% copy of the code inside the meta.php and besides that it is not a solution/answer to the question asked. –  ialocin Sep 17 '13 at 10:12

Another possible solution would be following what @toscho described in his answer on the following question:

How to use a specific category archive index as the site front page?

He is suggesting to create a front-page.php template which uses the according category template. Besides that he has a solution to prevent duplicate content, which is something you (should) want.

The answer is for a different use case, but it will get you almost there. Of course it is necessary to adapt that. For this some knowledge about templates is needed:

Woocommerce - Template Structure
Wordpress - Template Hierarchy

Because the woocommerce product categories - named: product_cat - are a custom taxonomie the following section applies to them:

Wordpress - Template Hierarchy - Custom Taxonomies

The template files in the woocommerce structure are taxonomy-product_cat.php and/or - because the first one is just locating this one - archive-product.php.

share|improve this answer

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.

share|improve this answer

protected by Community Apr 8 at 18:00

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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