Created
March 21, 2015 21:20
-
-
Save UltimateWoo/a0b25a1b28079ab04b83 to your computer and use it in GitHub Desktop.
Custom query and loop for WooCommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( is_shop() || is_product_category() || is_product_tag() ) { // Only run on shop archive pages, not single products or other pages | |
// Products per page | |
$per_page = 24; | |
if ( get_query_var( 'taxonomy' ) ) { // If on a product taxonomy archive (category or tag) | |
$args = array( | |
'post_type' => 'product', | |
'posts_per_page' => $per_page, | |
'paged' => get_query_var( 'paged' ), | |
'tax_query' => array( | |
array( | |
'taxonomy' => get_query_var( 'taxonomy' ), | |
'field' => 'slug', | |
'terms' => get_query_var( 'term' ), | |
), | |
), | |
); | |
} else { // On main shop page | |
$args = array( | |
'post_type' => 'product', | |
'orderby' => 'date', | |
'order' => 'DESC', | |
'posts_per_page' => $per_page, | |
'paged' => get_query_var( 'paged' ), | |
); | |
} | |
// Set the query | |
$products = new WP_Query( $args ); | |
// Standard loop | |
if ( $products->have_posts() ) : | |
while ( $products->have_posts() ) : $products->the_post(); | |
endwhile; | |
wp_reset_postdata(); | |
endif; | |
} else { // If not on archive page (cart, checkout, etc), do normal operations | |
woocommerce_content(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment