[Woocommerce] How to Display Products from Specific Category

These code to show product with ‘shoe’ category.
You can change ‘shoe’ with any category you want.

‘orderby’ => ‘rand’ mean show the product as random.

orderby (string | array) – Sort retrieved posts by parameter. Defaults to ‘date (post_date)’. One or more options can be passed.
‘none’ – No order (available since Version 2.8).
‘ID’ – Order by post id. Note the capitalization.
‘author’ – Order by author. (‘post_author’ is also accepted.)
‘title’ – Order by title. (‘post_title’ is also accepted.)
‘name’ – Order by post name (post slug). (‘post_name’ is also accepted.)
‘type’ – Order by post type (available since Version 4.0). (‘post_type’ is also accepted.)
‘date’ – Order by date. (‘post_date’ is also accepted.)
‘modified’ – Order by last modified date. (‘post_modified’ is also accepted.)
‘parent’ – Order by post/page parent id. (‘post_parent’ is also accepted.)
‘rand’ – Random order. You can also use ‘RAND(x)’ where ‘x’ is an integer seed value.

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

Ref: https://wordpress.stackexchange.com/questions/67247/how-to-display-product-specific-to-a-category-with-woocommerce-plugin

Related Posts

Recommended File Permissions for WordPress

July 31, 2017

Security, Tutorial, WordPress

What permissions should I have for the following: Root folder storing all the WordPress content wp-admin wp-content wp-includes On computer filesystems, different files and directories have permissions that specify who and what can read, write, modify and access them. This is important because WordPress may need access to write to files in your wp-content directory to enable certain functions. […]

Read More

How to Prevent SQL injection in PHP 2017

July 31, 2017

PHP, Security, Tutorial

SQL injection happens when you interpolate some content into a SQL query string, and the result modifies the syntax of your query in ways you didn’t intend. It doesn’t have to be malicious, it can be an accident. But accidental SQL injection is more likely to result in an error than in a vulnerability. The […]

Read More