2017-04-09 206 views
0

我的项目需要使用分页在一个页面中列出所有分类。分页wordpress中的分类列表

例如:我有100个类别,需要为每个页面显示10个类别,下面是数字分页。

如何在Wordpress中做到这一点?

$taxonomy = 'category'; 
    if(!isset($_GET['showall'])): 

     $total_terms = wp_count_terms('category'); 
     $pages = ceil($total_terms/$per_page); 
     // if there's more than one page 
     if($pages > 1): 
      echo '<ul>'; 

      for ($pagecount = 1; $pagecount <= $pages; $pagecount++): 
       echo '<li><a href="'.get_term_link('news', $taxonomy).'page/'.$pagecount.'/">'.$pagecount.'</a></li>'; 
      endfor; 

      echo '</ul>'; 
     endif; 

    else: 

    endif; 

它返回作为域/类别/新闻/页面/号码,但我点击页码,它返回到主页。

+0

您能向我们展示您尝试过的代码吗?请不要指望别人为你编写完整的代码。为这项工作聘请一名自由职业者。用户在这里遇到特定的与编程有关的问题时,自愿帮助其他用户。 –

+0

我更新了我的代码到目前为止 – ngocdung

回答

0

最后我做了我的自我... 这里是代码,所以随意使用,如果你想。

   <?php 
        $category = get_category(get_query_var('cat')); 
        $cat_id = $category->cat_ID; 
        $category_per_page = 2; 
        $page = 1; 
        $offset = 0; 

        $taxonomy = 'category'; 
        if (!empty($_GET['page'])){ 
         $offset = ($_GET['page'] - 1) * $category_per_page; 
        } 

        $tax_terms = get_terms($taxonomy, array('parent' => $cat_id,'number' => $category_per_page, 'offset' => $offset , 'hide_empty' => false)); 
        $totalCategories = get_terms($taxonomy, array('parent' => $cat_id, 'hide_empty' => false)); 
        $totalNumber = sizeof($totalCategories); 
        $totalPagination = round($totalNumber/$category_per_page); 

        foreach ($tax_terms as $tax_term) : 
         $args=array('cat' => $tax_term->term_id,'showposts'=>6); 
         $query=new WP_Query($args); 
       ?> 
       <div class="col-xs-12"> 
        <div class="heading"> 
         <h3 class="heading-title"><a href="<?=esc_attr(get_term_link($tax_term, $taxonomy));?>" title="<?=the_title();?>"><?= $tax_term->name;?></a></h3> 
        </div> 
        <section class="list-items owl-carousel owl-theme owl-loaded owl-drag" id="projects-slide"> 
         <?php while($query->have_posts()):$query->the_post();?> 
         <div class="item"> 
          <figure class="item-featured-img"> 
           <a href="<?=the_permalink();?>"> 
            <?php if (has_post_thumbnail()) : 
             the_post_thumbnail('thumbnail',array('class' => 'img-responsive center-block')); 
            else : ?> 
             <img class="img-responsive center-block" src="<?php bloginfo('template_url');?>/img/no-img.jpg" alt="<?php echo $alt_text; ?>"> 
            <?php endif ?> 
           </a> 
           <figcaption class="title-item"> 
            <h2><a href="<?=the_permalink();?>" title="<?=the_title();?>"><?=the_title();?></a></h2> 
           </figcaption> 
          </figure> 
          <div class="excerpt"> 
           <p><?php echo cut_string(get_the_content(),65,'');?></p> 
          </div> 
         </div> 
         <?php endwhile;?> 
        </section> 
       </div> 
       <?php endforeach;?> 

       <form method="GET"> 
        <?php for ($i=0; $i < $totalPagination ; $i++) { ?> 
        <button name="page" value="<?= $i+1 ?>"><?= $i+1 ?></button> 
        <?php } ?> 
       </form>