【问题标题】:Wordpress get list of child categories with paginationWordpress 获取带有分页的子类别列表
【发布时间】:2015-11-23 06:19:47
【问题描述】:

对不起,我的英语很差。 我有 9 个子类别,我用分页显示在父类别页面上。我每页设置5个类别。因此,对于 9 个类别,它应该是 2 页。 比如 page/1、page/2。但对我来说,它继续到第 3 页、第 4 页等等。 当没有更多类别时,如何停止分页。 https://iwmagazineme.com/category/guides/

这是我的 parent-category.php 代码

<?php 
            if (is_category()) {
            $this_category = get_category($cat);
            if (get_category_children($this_category->cat_ID) != "") {
            echo '<div class="guides-categories"><ul>';

            $catpage = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
            $catnum = 5;
            $offset = ($catnum * $catpage) - 5;

            $childcategories = get_categories(array(
           'orderby' => 'id',
            'order' => 'DESC',
            'hide_empty' => '0',
            'number' => $catnum,
            'offset' => $offset,
            'child_of' => $this_category->cat_ID
            ));
            ?>

            <?php
            foreach($childcategories as $category) {
            ?>

            <div class="row nopadding-guides">
            <?php echo '<a href="' . get_category_link( $category->term_id ) . '">';  ?>

                <div class="col-md-6 guides-item"> 
                    <?php $image = get_field('guide_feature_image', 'category_'.$category->term_id);
                    echo '<img src="' . $image['url'] . '" />'; 
                    ?>
                </div>

                <div class="col-md-6 guides-item"> 
                    <h2> <?php echo $category->name; ?> </h2>
                    <h5>by <?php echo get_field('guide_writer_name', 'category_'.$category->term_id); ?> </h5>
                    <p style="/*height: 152px;overflow-y: auto;*/margin-bottom: 0px;line-height: 23px;">  <?php echo get_field('guide_description', 'category_'.$category->term_id); ?> </p>
                </div>
            <?php echo '</a>'; ?>   

            </div>


            <?php  } ?>

            <div class="guides-pagination">
                <div class="next-page"> <?php next_posts_link( 'Next', ''); ?> </div>
                <div class="previous-page"> <?php previous_posts_link( 'Previous', ''); ?> </div>
            </div>

            <?php echo '</ul></div>';
                }
            }
            ?>

【问题讨论】:

  • 您在 $childcategories 数组中缺少“分页”。
  • 仍然显示下一个链接。

标签: wordpress pagination


【解决方案1】:

您缺少 $childcategories 数组中的 'paged' 和 'posts_per_page':

<?php 
            if (is_category()) {
            $this_category = get_category($cat);
            if (get_category_children($this_category->cat_ID) != "") {
            echo '<div class="guides-categories"><ul>';

            $catpage = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
            $catnum = 5;
            $offset = ($catnum * $catpage) - 5;

            $childcategories = get_categories(array(
           'orderby' => 'id',
            'order' => 'DESC',
            'hide_empty' => '0',
            'number' => $catnum,
            'offset' => $offset,
            'child_of' => $this_category->cat_ID,
            'posts_per_page' => 5,
            'paged' => $catpage
            ));
            ?>

            <?php
            foreach($childcategories as $category) {
            ?>

            <div class="row nopadding-guides">
            <?php echo '<a href="' . get_category_link( $category->term_id ) . '">';  ?>

                <div class="col-md-6 guides-item"> 
                    <?php $image = get_field('guide_feature_image', 'category_'.$category->term_id);
                    echo '<img src="' . $image['url'] . '" />'; 
                    ?>
                </div>

                <div class="col-md-6 guides-item"> 
                    <h2> <?php echo $category->name; ?> </h2>
                    <h5>by <?php echo get_field('guide_writer_name', 'category_'.$category->term_id); ?> </h5>
                    <p style="/*height: 152px;overflow-y: auto;*/margin-bottom: 0px;line-height: 23px;">  <?php echo get_field('guide_description', 'category_'.$category->term_id); ?> </p>
                </div>
            <?php echo '</a>'; ?>   

            </div>


            <?php  } ?>

            <div class="guides-pagination">
                <div class="next-page"> <?php next_posts_link( 'Next', ''); ?> </div>
                <div class="previous-page"> <?php previous_posts_link( 'Previous', ''); ?> </div>
            </div>

            <?php echo '</ul></div>';
                }
            }
            ?>

【讨论】:

  • 从你的数组中删除 hide_empty 并告诉我前面显示的内容。
  • 已删除但仍显示到第 3 页。 iwmagazineme.com/category/guides
  • 看来是你的问题。你那里有帖子。在第三页和第四页。
  • 是的,对于每个子类别,我都有很多帖子。当我添加帖子时,我也选择了父类别和子类别。我应该只选择孩子吗?
猜你喜欢
  • 1970-01-01
  • 2022-11-16
  • 1970-01-01
  • 2020-03-27
  • 2015-11-27
  • 2021-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多