dongmiao520892 2017-04-09 06:09
浏览 25

分页wordpress中的类别列表

My project need to list all categories in one page with pagination.

Example: I had 100 categories and need to show 10 categories for each page with number pagination below.

How can I do that in 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;

It return as domain/category/news/page/number but i click to page number it's return to homepage.

  • 写回答

1条回答 默认 最新

  • ds2128629 2017-04-09 14:44
    关注

    Finally i did it by my self... Here is the code , so feel free to use if u want.

                   <?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>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)