分页类别列表wordpress


Paginate Category list wordpress

如何将这个类别列表放入分页中,因为目前我有一个代码可以显示所有类别列表,包括按类别发布。问题是,如果类别列表超过10,如何使其分页。

 $cats = get_categories("exclude=1,5,15"); 
            foreach ($cats as $cat) {
                    // setup the cateogory ID
                        $cat_id= $cat->term_id;
                        $cat_child =$cat -> category_parent;
                    // Make a header for the cateogry
                        echo "<div class='anchor2' id='cat_".$cat_id. "'></div>";
                        echo "<div id='". $cat_child ."'class='cat_id".$cat_id." cat-cont large-6 medium-6 columns pad25px'>";
                        echo "<h5 class='title-post cat_id_" . $cat_id ."' >".$cat->name."</h5>";
                        echo "<a href='dev/all/#cat_".$cat_id."' class='see-more' target='_blank'>See more >></a>";
                        // create a custom wordpress query
                        query_posts("cat=$cat_id&posts_per_page=5&depth=1");
                        // start the wordpress loop!
                        if (have_posts()) : while (have_posts()) : the_post(); ?>
                            <?php // create our link now that the post is setup ?>
                            <?php get_template_part( 'parts/loop', 'archive-grid' ); ?>
                        <?php endwhile; ?>
                        <?php else : ?>             
                                <?php get_template_part( 'parts/content', 'missing' ); ?>
                        <?php  endif; // done our wordpress loop. Will start again for each category ?>
            <?php echo "</div>";}// done the foreach statement ?>

您在查询中根本没有使用$paged变量。更换您的代码部分

// create a custom wordpress query
query_posts("cat=$cat_id&posts_per_page=5&depth=1");

带有

if ( get_query_var('paged') ) {
        $paged = get_query_var('paged');
    } else if ( get_query_var('page') ) {
        $paged = get_query_var('page');
    } else {$paged = 1;}
$args = array(
'cat' => $cat_id,
'posts_per_page' => 5,
'paged' => $paged
);
query_posts($args);

而且它对你来说很好,另外你也可以使用

<div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
  <div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
</div>

为了实现完全控制,如果"下一步"answers"上一步"按钮有效,那么您可以对这一部分进行注释,并可以使用代码进行分页,因为您希望显示页码等(如果您不想使用"下一个"answers"前一个"按钮等)

对于那些正在寻找答案的人:如果您在一个页面中显示所有类别,下面是如何对类别列表进行分页。

   if ( get_query_var('paged') ) {
                        $paged = get_query_var('paged');
                    } else if ( get_query_var('page') ) {
                        $paged = get_query_var('page');
                    } else {$paged = 1;}
             $per_page = 4;
             $paged_offset = ($paged - 1) * $per_page;
             $paginate = array(
                            'orderby'           => 'name',
                            'order'             => 'ASC',
                            'hide_empty'        => 0,
                            'number'            => $per_page,
                            'paged'             => $paged,
                            'exclude'           => array(1,5,15,18),
                            'offset'            => $paged_offset
                        );  
            // get all the categories from the database
             $cats = get_categories($paginate);
             echo '<div class="page1">';
            foreach ($cats as $cat) {
                        // setup the cateogory ID
                        $cat_id= $cat->term_id;
                        $cat_child =$cat -> category_parent;

                        // Make a header for the cateogry
                        echo "<div class='anchor2' id='cat_".$cat_id. "'></div>";
                        echo "<div id='". $cat_child ."'class='cat_id".$cat_id." cat-cont large-6 medium-6 columns pad25px'>";
                        echo "<h5 class='title-post cat_id_" . $cat_id ."' >".$cat->name."</h5>";
                        echo "<a href='dev/all/#cat_".$cat_id."' class='see-more' target='_blank'>See more >></a>";
                     //   echo "<h2 id='". $cat_child ."'class='title-post cat_id_" . $cat_child ."' >".$cat->name."</h2>";
                        // create a custom wordpress query


                $args = array(
                    'cat' => $cat_id,
                    'posts_per_page' => 5,
                );
                query_posts($args);
                    query_posts($args);
                        // start the wordpress loop!
                        if (have_posts()) : while (have_posts()) : the_post(); ?>
                            <?php // create our link now that the post is setup ?>
                            <?php get_template_part( 'parts/loop', 'archive-grid' ); ?>
                        <?php endwhile; ?>
                        <?php else : ?>             
                                <?php get_template_part( 'parts/content', 'missing' ); ?>
                        <?php  endif; // done our wordpress loop. Will start again for each category ?>

            <?php echo "</div>"; }// done the foreach statement 

            ?>
             <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
             <div class="alignright"><?php next_posts_link('More &raquo;') ?></div>