最近产品woocommerce的数量分页

时间:2017-08-13 11:03:48

标签: php wordpress woocommerce pagination

我是wordpress的新手,我找到了显示最新产品的代码,但我不知道如何为它添加数字分页。

<?php
    $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 4,'nopaging' => false, 'orderby' =>'date','order' => 'DESC' );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); global $product;
?>
<li class="span3">
    <a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
        <?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>
    </a>
    <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</li>
<!-- /span3 -->
<?php endwhile; ?>
<?php wp_reset_query(); ?>

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

将此功能添加到你的functions.php

function theme_pagination($pages = '', $range = 3)
{

      global $wp_query;
      if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
      elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
      else { $paged = 1; }

      $showitems = ($range * 2)+1;  

      if(empty($paged)) $paged = 1;

      if($pages == '' && $pages != 0)
      {
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages)
         {
             $pages = 1;
         }
     }   

     if(1 != $pages)
     {
         echo "<div class='pagination'>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo;</a>";
         if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</a>";

         for ($i=1; $i <= $pages; $i++)
         {
             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
             {
                 echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
             }
         }

         if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>&rsaquo;</a>";  
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>&raquo;</a>";
         echo "</div>\n";
     }

}

添加全局$ wp_query;

并在<?php theme_pagination($wp_query->max_num_pages); ?>

之后使用<?php wp_reset_query(); ?>