本文介绍了没有插件的woocomerece的分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在woocomerece的分页方面遇到了问题.我正在使用mystyle主题和woocomerece插件.
I have facing a problem with pagination in woocomerece.I am using mystyle theme and with woocomerece plugin.
我想每页显示12个产品.我的代码不起作用
i want to display 12 product per page.my code is not working
这是我的代码.
<?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>';
}
}
?>
推荐答案
以下是带有代码的解决方案.
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; ?>
这篇关于没有插件的woocomerece的分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!