Last active
September 27, 2015 16:42
-
-
Save jackgregory/55cda1647c4cbc61a698 to your computer and use it in GitHub Desktop.
Add pagination to WooCommerce product shortcodes
This file contains 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
function c4a8a13f4e5_woocommerce_shortcode_products_query( $query_args, $atts ) { | |
$query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; | |
return $query_args; | |
} | |
add_filter( 'woocommerce_shortcode_products_query', 'c4a8a13f4e5_woocommerce_shortcode_products_query', 10, 2 ); | |
function c4a8a13f4e5_shortcodes_pagination() { | |
woocommerce_pagination(); | |
} | |
$product_shortcodes = array( 'recent_products', 'sale_products', 'product_category', 'best_selling_products', 'products' , 'top_rated_products', 'featured_products' ); | |
foreach( $product_shortcodes as $product_shortcode ) { | |
add_action( "woocommerce_shortcode_after_{$product_shortcode}_loop", 'c4a8a13f4e5_shortcodes_pagination' ); | |
} | |
function c4a8a13f4e5_loop_end( $q ) { | |
// only effect wp_query on post, pages or custom post types | |
if( ! is_singular() ) { | |
return; | |
} | |
// exit if the query is not for products | |
if ( ! $q->is_post_type_archive || 'product' !== $q->query['post_type'] ) { | |
return; | |
} | |
global $wp_query; | |
// so pagination shows correct number of pages | |
$wp_query->max_num_pages = $q->max_num_pages; | |
} | |
add_action( 'loop_end', 'c4a8a13f4e5_loop_end' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment