给使用Woocommerce短码生成的产品列表页面添加翻页功能

给使用Woocommerce短码生成的产品列表页面添加翻页功能

作者:DIYzhan.com |  浏览:6561 |  评论已关闭 条评论
Web Hosting

woo-plugin
Woocommerce功能是很强大的,我们除了在后台可以设置woocommerce的各项功能,我们还可以使用woocommerce提供的短码,在新建的页面实现各种功能,建立各样的产品分类页面;

但是,如果我们使用woocommerce提供的短码 [featured_products per_page=”12″ columns=”4″] 或者 [recent_products per_page=”12″ columns=”4″] 在某个页面来展现featured products或者recent products的话,我们是只可以在该页面显示最多3行4列共12个产品(当然你可以改变上述短码中显示的数量,在一页显示出来),如果我们有更多的featured products或者recent products的话,并通过翻页显示在第2,3,4…..等页面应该怎么操作呢?

注:大家可以在 https://docs.woothemes.com/document/woocommerce-shortcodes/ 页面查看woocommerce的相关短码。另外,直接使用插件:Shortcode Pagination for WooCommerce ,也可以添加翻页功能,而不需要下面的步骤了。

这个时候,我们需要在主题的functions.php文件里面添加如下代码,并保存即可。(注意:修改functions.php之前请备份你的functions.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() {
woocommerce_pagination();
}

}// END WOOCOMMERCE
}// END FRONTPAGE

原创文章,转载请注明: 转载自DIYzhan.com-从零开始自己做外贸网站和海外网络营销
本文链接地址:https://www.diyzhan.com/2015/11/woocommerce-pagenavi/

相关推荐
更多

评论已关闭。

长按扫我微信
需要帮助?
点击这里给我发消息