我正在使用 woocommerce产品类别短代码 [product_categories],并希望分页,
因为我在产品系列页面上显示类别 - 目前只显示了4个类别。
我已添加此短代码[product_categories per_page="4" columns="4"]
。但它只显示4个类别。但没有显示分页。我尝试了 WP-PageNavi插件,并在我的主题函数中添加了以下代码.php
remove_action('woocommerce_pagination', 'woocommerce_pagination', 10);
function woocommerce_pagination() {
wp_pagenavi();
}
add_action( 'woocommerce_pagination', 'woocommerce_pagination', 10);
它没有用。有没有办法在woocommerce中添加分页?
答案 0 :(得分:0)
请尝试以下代码。它对我有用:
if ( ! is_admin() ) {
if ( defined('WC_VERSION') ) {
// Set Pagination for shortcodes custom loop on single-pages.
// @uses $woocommerce_loop;
add_action( 'pre_get_posts', 'hs_wc_pre_get_posts_query' );
function hs_wc_pre_get_posts_query( $query ) {
global $woocommerce_loop;
// Get paged from main query only
// ! frontpage missing the post_type
if ( is_main_query() && ( $query->query['post_type'] == 'product' ) || ! isset( $query->query['post_type'] ) ){
if ( isset($query->query['paged']) ){
$woocommerce_loop['paged'] = $query->query['paged'];
}
}
if ( ! $query->is_post_type_archive || $query->query['post_type'] !== 'product' ){
return;
}
$query->is_paged = true;
$query->query['paged'] = $woocommerce_loop['paged'];
$query->query_vars['paged'] = $woocommerce_loop['paged'];
}
/** Prepare Pagination data for shortcodes on pages
* @uses $woocommerce_loop;
**/
add_action( 'loop_end', 'hs_query_loop_end' );
function hs_query_loop_end( $query ) {
if ( ! $query->is_post_type_archive || $query->query['post_type'] !== 'product' ){
return;
}
// Cache data for pagination
global $woocommerce_loop;
$woocommerce_loop['pagination']['paged'] = $woocommerce_loop['paged'];
$woocommerce_loop['pagination']['found_posts'] = $query->found_posts;
$woocommerce_loop['pagination']['max_num_pages'] = $query->max_num_pages;
$woocommerce_loop['pagination']['post_count'] = $query->post_count;
$woocommerce_loop['pagination']['current_post'] = $query->current_post;
}
/**
* Pagination for shortcodes on single-pages
* @uses $woocommerce_loop;
*/
add_action( 'woocommerce_after_template_part', 'hs_wc_shortcode_pagination' );
function hs_wc_shortcode_pagination( $template_name ) {
if ( ! ( $template_name === 'loop/loop-end.php' && is_page() ) ){
return;
}
global $wp_query, $woocommerce_loop;
if ( ! isset( $woocommerce_loop['pagination'] ) ){
return;
}
$wp_query->query_vars['paged'] = $woocommerce_loop['pagination']['paged'];
$wp_query->query['paged'] = $woocommerce_loop['pagination']['paged'];
$wp_query->max_num_pages = $woocommerce_loop['pagination']['max_num_pages'];
$wp_query->found_posts = $woocommerce_loop['pagination']['found_posts'];
$wp_query->post_count = $woocommerce_loop['pagination']['post_count'];
$wp_query->current_post = $woocommerce_loop['pagination']['current_post'];
// Custom pagination function or default woocommerce_pagination()
hs_woocommerce_pagination();
}
/**
* Custom pagination for WooCommerce instead the default woocommerce_pagination()
* @uses plugin Prime Strategy Page Navi, but added is_singular() on #line16
**/
remove_action('woocommerce_after_shop_loop', 'woocommerce_pagination', 10);
add_action( 'woocommerce_after_shop_loop', 'hs_woocommerce_pagination', 10);
function hs_woocommerce_pagination() {
woocommerce_pagination();
}
}
}
https://gist.github.com/alexandreleroux/ba8aba53441967638c87