WooCommerce自定义循环首页分页

时间:2016-03-24 作者:Cookie_Pa

我有一个自定义页面模板,我用它作为我的主题的首页。在那个页面中,我有一个部分列出了使用自定义循环的woocommerce产品。我想在该部分插入下一篇文章的页码。我正在使用next\\u posts\\u link()函数生成下一页url链接。它完美地实现了这一点,但在我的主页上/2并没有显示新产品,而是显示旧产品。

这是我的代码:

    $paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;

    $args = array(
       \'post_type\'      => \'product\',
       \'posts_per_page\' => $top_selling_products_nr,
       \'meta_key\'       => \'total_sales\',
       \'paged\'          => $paged,
       \'orderby\'        => \'meta_value_num\'
    );

    <?php if ( $woo_home_query->have_posts() ) : ?>

        <div class="products container grid-wrapper clear">

            <div class="row">
            <?php

                while ( $woo_home_query->have_posts() ) : $woo_home_query->the_post();
                    wc_get_template_part( \'content\', \'product\' );
                endwhile;

            ?>
            </div>

            <?php if ( $woo_home_query->max_num_pages > 1 ) : ?>

                <nav class="navigation is posts-navigation" role="navigation">
                    <h2 class="screen-reader-text"><?php esc_html_e( \'Posts navigation\', \'goodz\' ); ?></h2>
                    <div id="infinite-handle" class="nav-links">
                       <div class="nav-previous"><?php next_posts_link( esc_html__( \'Load More\', \'goodz\' ), $woo_home_query->max_num_pages ); ?></div>
                    </div> <!-- .nav-links -->
                </nav> <!-- .navigation -->

            <?php endif; ?>

        </div>

    <?php endif; ?>

1 个回复
SO网友:Purvik Dhorajiya

你能试试下面的代码吗?

$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;

    $woo_home_query = array(
       \'post_type\'      => \'product\',
       \'posts_per_page\' => $top_selling_products_nr,
       \'meta_key\'       => \'total_sales\',
       \'paged\'          => $paged,
       \'orderby\'        => \'meta_value_num\'
    );

    <?php if ( $woo_home_query->have_posts() ) : ?>

        <div class="products container grid-wrapper clear">

            <div class="row">
            <?php

                while ( $woo_home_query->have_posts() ) : $woo_home_query->the_post();
                    wc_get_template_part( \'content\', \'product\' );
                endwhile;

            ?>
            </div>

            <?php 
            $total_pages = $woo_home_query->max_num_pages;

            if ($total_pages > 1){

                $current_page = max(1, get_query_var(\'paged\'));

                echo paginate_links(array(
                    \'base\' => get_pagenum_link(1) . \'%_%\',
                    \'format\' => \'/page/%#%\',
                    \'current\' => $current_page,
                    \'total\' => $total_pages,
                    \'prev_text\'    => __(\'« prev\'),
                    \'next_text\'    => __(\'next »\'),
                ));
            }
            ?>  

        </div>

    <?php endif; ?>

相关推荐

Add pagination to WP_Query

我试图在下面的代码中添加分页,但不知道如何与之集成。请帮忙在帖子上找到页码。<?php $child_query = new WP_Query(array(\'post_type\' => \'blogpost\', \'orderby\' => \'date\', \'order\' => \'DESC\')); while ( $child_query->ha