【问题标题】:Pagination With woocomerece without plugins无需插件的 woocommerce 分页
【发布时间】:2015-02-08 08:24:00
【问题描述】:

我在 woocomerece 中遇到了分页问题。 我正在使用 mystyle 主题和 woocomerece 插件。

我想每页显示 12 个产品。我的代码不起作用

这是我的代码。

<?php

    $per_page =12;
    $numpost = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'product'"); //calculating the number of products
    $totalpages= ceil($numpost/$per_page);//calculating the last page or total pages
    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
    //$page =(isset($_GET['page'])) ? (int)$_GET['page'] : 1 ;
    $start=($page-1)* $per_page;
    $limit="limit".($page-1)*$per_page.",$per_page";
    $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => $per_page, 'orderby' =>'date','order' => 'DESC'  );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

        <li class="rcollproli">    

            <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />'; ?>

            <h3><?php the_title(); ?></h3>

            <span class="price"><?php echo $product->get_price_html(); ?></span>

            <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
            <?php woo_pagination(); ?>
        </li><!-- /span3 -->
<?php endwhile;  ?>

<?php 
    if($totalpages >=1){

    for($x=1;$x<=$totalpages;$x++)
    {
       echo '<a href="?page_id=19='.$x.'">'.$x.'</a>';  
    }

    }
?>

【问题讨论】:

    标签: wordpress pagination woocommerce


    【解决方案1】:

    这是带有代码的解决方案。

    global $wpdb;
    
    $per_page =12;
    $numpost = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'product'"); //calculating the number of products
    //$query= $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE post_type = 'product' orderby ='date'");
    
    $totalpages= ceil($numpost/$per_page);
    
    $page =(isset($_GET['page'])) ? (int)$_GET['page'] : 1 ;
    
    
    $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => $per_page,'paged' => get_query_var('paged'), 'orderby' =>'date','order' => 'DESC'  );
    
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
    
         <li class="rcollproli">    
    
            <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />'; ?>
    
                <h3><?php the_title(); ?></h3>
    
                   <span class="price"><?php echo $product->get_price_html(); ?></span>
    
            <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
            <?php woo_pagination(); ?>
        </li><!-- /span3 -->
    <?php endwhile;  ?>
    

    【讨论】:

      【解决方案2】:

      默认情况下,WooCommerce 使用与博客帖子相同的“每页帖子”设置。

      但是,您可以将其过滤为您喜欢的任何值。

      add_filter( 'loop_shop_per_page', 'so_27395967_products_per_page' );
      function so_27395967_products_per_page(){
          return 12;
      }
      

      我认为您不需要自定义数据库查询。

      【讨论】:

      • 感谢 helgatheviking。我需要在我的产品页面上应用分页。你知道确切的代码吗?我通过这个代码从 db 中挑选产品。 $args = array('post_type' => 'product', 'stock' => 1, 'posts_per_page' => 12, 'orderby' =>'date','order' => 'DESC' );然后将 $args 放入。 $loop = new WP_Query( $args );它工作正常并显示 12 种产品。现在我需要为该页面添加分页。我是这项工作的新手。如果您对自定义循环的分页有任何了解。请告诉我。提前致谢。
      猜你喜欢
      • 2012-09-27
      • 1970-01-01
      • 1970-01-01
      • 2014-08-04
      • 2021-09-05
      • 2012-06-05
      • 2017-02-01
      • 2015-11-18
      • 2012-10-23
      相关资源
      最近更新 更多