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>';
}
}
?>
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.
I don't think you need a custom database query.
Here is the solution with code.