Skip to content

Instantly share code, notes, and snippets.

@davidvandenbor
Last active July 24, 2016 22:31
Show Gist options
  • Save davidvandenbor/748c70113da0a6714d5bc64e195e225c to your computer and use it in GitHub Desktop.
Save davidvandenbor/748c70113da0a6714d5bc64e195e225c to your computer and use it in GitHub Desktop.
WooCommerce custom Query Loop
<?php
/** ---------------------------------------------------------------------------- //
* @group WooCommerce custom Query Loop
* @author @david
*/
?>
<ul class="products">
<?php
$args = array('post_type' => 'product', 'posts_per_page' => 50, 'product_cat' => 'trousers', 'orderby' => 'rand');
$loop = new WP_Query($args);
while ($loop->have_posts()): $loop->the_post();global $product;?>
<h2>Trousers</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-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment