分页与没有插件的woocommerce

时间:2014-12-10 08:04:24

标签: wordpress pagination woocommerce

我在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>';  
    }

    }
?>

2 个答案:

答案 0 :(得分:0)

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

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

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

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

答案 1 :(得分:0)

以下是带代码的解决方案。

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