【问题标题】:Pagination in WooCommerceWooCommerce 中的分页
【发布时间】:2014-08-04 16:44:34
【问题描述】:

我正在使用此简码在页面上显示产品类别。但是当我们使用它时,它不会显示分页。因为我们的类别中有很多产品。

我们正在使用以下代码:

[product_category category="snowpeak" per_page="12" columns="4" orderby="date" order="desc"]

【问题讨论】:

  • 此注释写在 woocommerce 简码文档上。请注意:“per_page”短代码参数将确定页面上显示的产品数量。这不会为短代码添加分页。
  • 你已经导入了woopages?

标签: php mysql woocommerce


【解决方案1】:

您可以在此处投票支持将此功能实施到 WooCommerce 短代码中: http://ideas.woothemes.com/forums/133476-woocommerce/suggestions/4146798-add-pagination-support-for-list-of-products-render

否则,您可以创建自定义产品循环 (http://docs.woothemes.com/document/sample-products-loop/) 并在 WP_Query 中添加自定义 arg 以进行分页:

例如。

    <ul class="products">
        <?php
        global $paged;

            $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
            $args = array(
                'post_type' => 'product',
                'posts_per_page' => 4,
                'paged' => $paged


                );
            $loop = new WP_Query( $args );
            if ( $loop->have_posts() ) {
                while ( $loop->have_posts() ) : $loop->the_post();
                    woocommerce_get_template_part( 'content', 'product' );
                endwhile;
            } else {
                echo __( 'No products found' );
            }
        ?>


    <nav>
        <ul>
            <li><?php previous_posts_link( '&laquo; PREV', $loop->max_num_pages) ?></li> 
            <li><?php next_posts_link( 'NEXT &raquo;', $loop->max_num_pages) ?></li>
        </ul>
    </nav>

    <?php wp_reset_postdata(); ?>
    </ul><!--/.products-->

【讨论】:

  • 对我来说是检索名为'paged'的变量:$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  • 干得好!这为我修好了
【解决方案2】:

您可以在产品页面中使用与在帖子页面中使用的相同的分页。 举个例子

在functions.php文件中添加如下代码

    function custom_pagination() {
    global $wp_query;
    $big = 999999999;
    $pages = paginate_links(array(
        'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
        'format' => '?page=%#%',
        'current' => max(1, get_query_var('paged')),
        'total' => $wp_query->max_num_pages,
        'prev_next' => false,
        'type' => 'array',
        'prev_next' => TRUE,
        'prev_text' => '&larr; Previous',
        'next_text' => 'Next &rarr;',
            ));
    if (is_array($pages)) {
        $current_page = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
        echo '<ul class="pagination">';
        foreach ($pages as $i => $page) {
            if ($current_page == 1 && $i == 0) {
                echo "<li class='active'>$page</li>";
            } else {
                if ($current_page != 1 && $current_page == $i) {
                    echo "<li class='active'>$page</li>";
                } else {
                    echo "<li>$page</li>";
                }
            }
        }
        echo '</ul>';
    }
}

下一步是调用函数,在帖子或产品页面中进行分页工作

以 wp_query 为例

<?php 

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wp_query = new WP_Query("category_name='prayer'&paged=$paged&posts_per_page=12"); 
?>

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



    <!-- the loop -->
    <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>

        <?php get_template_part('prayer-loop'); ?>

    <?php endwhile; ?>
    <!-- end of the loop -->

    <div class="row" style="clear: both;">
        <?php custom_pagination(); ?>
    </div>


    <?php wp_reset_postdata(); ?>

<?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

如果您将引导程序添加到您的网站,此分页将非常有用。

【讨论】:

    【解决方案3】:
    <ul class="products">
          <?php
          global $paged;
              $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
              $args = array(
                  'post_type' => 'product',
                  'posts_per_page' => 4,
                  'paged' => $paged
    
    
                    );
                $loop = new WP_Query( $args );
                if ( $loop->have_posts() ) {
                    while ( $loop->have_posts() ) : $loop->the_post();
                        woocommerce_get_template_part( 'content', 'product' );
                    endwhile;
                } else {
                    echo __( 'No products found' );
                }
            ?>
    
    
        <nav>
            <ul>
                <li><?php previous_posts_link( '&laquo; PREV', $loop->max_num_pages) ?></li> 
                <li><?php next_posts_link( 'NEXT &raquo;', $loop->max_num_pages) ?></li>
            </ul>
        </nav>
    
        <?php wp_reset_postdata(); ?>
        </ul><!--/.products-->
    

    【讨论】:

      【解决方案4】:

      你可以试试这个。

      [products per_page="4" columns="2" paginate="true"]
      

      【讨论】:

      • 有效,但你需要限制使用它,例如:[products limit="12" paginate="true"]
      猜你喜欢
      • 2012-10-23
      • 2018-11-28
      • 2015-02-08
      • 1970-01-01
      • 1970-01-01
      • 2012-09-27
      • 2016-05-03
      • 1970-01-01
      相关资源
      最近更新 更多