如何在Woocommerce产品页面模板中添加自定义分页?

时间:2018-06-05 05:37:29

标签: templates

如何在Woocommerce产品页面模板中添加自定义分页?

这是我的代码:

但是分页不起作用。

有什么建议吗?

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

    $args = array( 
        'post_type' => 'product', 
        'posts_per_page' => 6, 
        'paged' => $paged, 
        'product_cat' => 'trekking,rafting', 
        'orderby' => 'ASC' 
        );

    $loop = new WP_Query( $args );

    while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

并显示分页:

<?php endwhile; ?>

<!-- End of the main loop -->

<!-- Add the pagination functions here. -->

<div class="cu-pagi">

<?php get_next_posts_link('Older', $loop->max_num_pages) ?>

<?php get_previous_posts_link('Newer', $loop->max_num_pages) ?>

</div>

    <?php wp_reset_query(); ?>
</div><!--/.products-->

1 个答案:

答案 0 :(得分:0)

得到答案:

工作代码:

<div class="cu-pagi">

<?php 
            $total_pages = $loop->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>