Pagination With woocomerece without plugins

2019-06-13 05:31发布

I have facing a problem with pagination in woocomerece. I am using mystyle theme and with woocomerece plugin.

i want to display 12 product per page.my code is not working

Here is my code.

<?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>';  
    }

    }
?>

2条回答
爷的心禁止访问
2楼-- · 2019-06-13 05:53

By default, WooCommerce uses the same "posts per page" setting that is used for blog posts.

But, you can filter it to be any value you like.

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

I don't think you need a custom database query.

查看更多
放荡不羁爱自由
3楼-- · 2019-06-13 06:05

Here is the solution with code.

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;  ?>
查看更多
登录 后发表回答