我正在使用最近的產品簡碼,[recent_products per_page="4" columns="2"]
並希望分頁,因爲我在主頁上顯示產品 - 目前只有4種產品正在顯示。有沒有辦法添加分頁?Woocommerce簡碼分頁
我輸入了虛擬數據,所以有45個產品。
我正在使用最近的產品簡碼,[recent_products per_page="4" columns="2"]
並希望分頁,因爲我在主頁上顯示產品 - 目前只有4種產品正在顯示。有沒有辦法添加分頁?Woocommerce簡碼分頁
我輸入了虛擬數據,所以有45個產品。
你可以考慮一個插件:WP-PageNavi。爲了與Woocommerce使用它,只需安裝和配置的插件,然後將下面的代碼添加到你的主題functions.php
:
remove_action('woocommerce_pagination', 'woocommerce_pagination', 10);
function woocommerce_pagination() {
wp_pagenavi();
}
add_action('woocommerce_pagination', 'woocommerce_pagination', 10);
來源:Mark van der Putten's blog,評論ommitted。
此外,您可以在專用StackExchange Wordpress site上提出這個問題。
在安裝此插件後,從這個GitHub的要點https://gist.github.com/klihelp/7810337包括下面的代碼到你的孩子爲主題的functions.php引用:https://wordpress.org/plugins/prime-strategy-page-navi/
<?php
/**
* This code shows pagination for WooCommerce shortcodes when it's embeded on single pages.
* Include into functions.php.
*/
if (! is_admin()) {
// ---------------------- FRONTPAGE -------------------
if (defined('WC_VERSION')) {
// ---------------------- WooCommerce active -------------------
/**
* Set Pagination for shortcodes custom loop on single-pages.
* @uses $woocommerce_loop;
*/
add_action('pre_get_posts', 'kli_wc_pre_get_posts_query');
function kli_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', 'kli_query_loop_end');
function kli_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', 'kli_wc_shortcode_pagination');
function kli_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()
kli_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', 'kli_woocommerce_pagination', 10);
function kli_woocommerce_pagination() {
wp_pagenavi();
}
}// END WOOCOMMERCE
}// END FRONTPAGE