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-->
admin
July 25, 2017
PHP, Tutorial, WooCommerce, WordPress
11 Comments