13

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.

closed as off-topic by Johannes Pille, birgire, TheDeadMedic, Pieter Goosen, Nicolai Sep 5 '14 at 11:19

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question should be specific to WordPress. Generic PHP/JS/HTML/CSS questions might be better asked at Stack Overflow or another appropriate site of the Stack Exchange network. Third party plugins and themes are off topic." – Johannes Pille, birgire, TheDeadMedic, Pieter Goosen, Nicolai
If this question can be reworded to fit the rules in the help center, please edit the question.

  • can u please tell me where to add this code if you please – Alaa M. Jaddou Jan 20 '17 at 19:49
26

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-->
  • 1
    @VanTong Then mark this answer as the answer: click the checkmark on the left side. – fuxia 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 '14 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 '14 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 – Alaa M. Jaddou Jan 20 '17 at 19:21
1

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.

-1

Try this

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

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