Woo Shop页码在不同的页面上使用

时间:2015-08-15 17:23:24

标签: wordpress woocommerce

我有一个主页和商店页面。我不想将我的主页设置为购物页面。

主页上充满了自定义内容和小部件,就在此内容之前,我希望在商店页面上列出与分页相同的产品。

这意味着我无法使用短代码显示产品,因为默认短代码不支持分页。

完成了所有文档并尝试了可能的方法。但它总会打破一些东西。

任何建议都会表示赞赏。

THX。

P.S。当你读到这篇文章的时候,我仍然在努力寻找解决方案,所以我很快就会发布一些内容:)

1 个答案:

答案 0 :(得分:1)

最后找到了很棒的解决方案here

我正在使用短代码[product_category per_page =“4”category =“category-slug”]

要激活分页,您需要激活此分页。 以下是需要粘贴到主题的functions.php中的确切代码段:

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

尝试这个尝试的好方法。接下来就是在不重页加载的情况下制作基于分页的ajax ......