如何在wordpress中以分页形式显示类别循环

时间:2014-09-11 08:19:33

标签: wordpress pagination categories

我想显示类别名称和类别缩略图,它需要在每页显示5个类别,在其他相应页面中显示其余部分。请任何人都有解决方案。

这是代码:

<?php 
$posts_per_page = 4;
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$offset = ( $paged - 1 );

global $paged;
$curpage = $paged ? $paged : 1;
$args = array(
'child_of' => 4,
'order_by' => 'name',
'paged' => $paged
);
$categories = get_categories( $args );  

foreach($categories as $category) { 
echo '' . $category->name.'';
 if(has_category_thumbnail($category->cat_ID)) {
   the_category_thumbnail($category->cat_ID);
 }
}

echo '
<div id="wp_pagination">        
<a class="previous page button" href="'.get_pagenum_link(($curpage-1 > 0 ? $curpage-1 :    1)).'">&lsaquo;</a>';
    for($i=1;$i<=$categories->max_num_pages;$i++)
        echo '<a class="'.($i == $curpage ? 'active ' : '').'page button" href="'.get_pagenum_link($i).'">'.$i.'</a>';
    echo '<a class="next page button" href="'.get_pagenum_link(($curpage+1 <= $categories->max_num_pages ? $curpage+1 : $categories->max_num_pages)).'">&rsaquo;</a>        
</div>
'; 

1 个答案:

答案 0 :(得分:1)

最后,我得到了上述问题的解决方案。我希望所有其他人都有同样的问题。

试试这段代码:

<?php
$args = array(            
        'child_of' => 4,
        'orderby' =>'date',
        'order' =>'ASC'
);
$categories = get_categories($args);
$numOfItems = 4;
$page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
$to = $page * $numOfItems ;
$current = $to - $numOfItems;
$total = sizeof($categories);
?>
<div id="ns-main">            
<?php
for ($i=$current; $i<$to; ++$i) {
    $category = $categories[$i]; 
  ?>
 <div class="ns-wrap">  
  <?php 
    if ($category->name) { 
      if(has_category_thumbnail($category->cat_ID)) {
      the_category_thumbnail($category->cat_ID);
       }
      echo '' . $category->name.'';
    }
  ?>
 </div> 
 <?php
}

 ?> 
</div>

    <div id="wp_pagination">    
<?php
unset($category);
echo paginate_links( array(
    'base' => add_query_arg( 'cpage', '%#%' ),
    'format' => '',
    'prev_text' => __('&laquo;'),
    'next_text' => __('&raquo;'),
    'total' => ceil($total / $numOfItems),
    'current' => $page
  ));

?>
</div>