wordpress Woocommerce 短代码分页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13193510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:04:20  来源:igfitidea点击:

Woocommerce shortcode pagination

wordpresswoocommerce

提问by jOpacic

I'm using Recent products shortcode, [recent_products per_page="4" columns="2"]and would like to have pagination, because I'm displaying products on homepage - and currently only 4 products are being displayed. Is there a way to add pagination?

我正在使用最近的产品短代码,[recent_products per_page="4" columns="2"]并希望有分页,因为我在主页上显示产品 - 目前只显示 4 个产品。有没有办法添加分页?

I have imported dummy data, so there are 45 products.

我导入了虚拟数据,所以有45个产品。

回答by s.d

You could consider a plugin: WP-PageNavi. In order to use it with Woocommerce, simply install and configure the plugin, then add the following code to your theme's functions.php:

你可以考虑一个插件:WP-PageNavi。为了将它与 Woocommerce 一起使用,只需安装和配置插件,然后将以下代码添加到您的主题functions.php

remove_action('woocommerce_pagination', 'woocommerce_pagination', 10);
function woocommerce_pagination() {
    wp_pagenavi();      
}
add_action( 'woocommerce_pagination', 'woocommerce_pagination', 10);

Source: Mark van der Putten's blog, comments ommitted.

来源:Mark van der Putten 的博客,已省略评论。

Also, you could ask this question on the dedicated StackExchange Wordpress site.

此外,您可以在专用的StackExchange Wordpress 站点上提出这个问题。

回答by Mahidul Isalm Mukto

Use [recent_products limit="12" columns="4" paginate="true"]This shortcode limit 12 means maximum 12 post on a page. more info woocommerce doc

使用[recent_products limit="12" columns="4" paginate="true"]此简码限制 12 意味着页面上最多 12 个帖子。更多信息woocommerce 文档

回答by Rahul Madhavan

Referencing from this github gist https://gist.github.com/klihelp/7810337Including the following code into your child-theme functions.php after installing this plugin: https://wordpress.org/plugins/prime-strategy-page-navi/

从此 github gist 引用https://gist.github.com/klihelp/7810337安装此插件后,将以下代码包含到您的 child-theme functions.php 中:https: //wordpress.org/plugins/prime-strategy-page -导航/

<?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

回答by Prog Beg

Use [recent_products limit="12" columns="4" paginate="true"]. This shortcode limit 12 means maximum 12 post on a page. more info woocommerce doc

使用[recent_products limit="12" columns="4" paginate="true"]. 此简码限制 12 意味着页面上最多 12 个帖子。更多信息 woocommerce 文档