Paginate类别列表wordpress

时间:2016-05-02 07:13:15

标签: php wordpress pagination categories

如何将此类别列表放入分页中,因为目前我的代码显示所有类别列表,包括按类别列出。 问题是如果类别列表超过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 ?>

2 个答案:

答案 0 :(得分:1)

您的查询中根本没有使用$ 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>

即使是完全控制,如果“下一个”和“上一个”按钮有效,那么您可以对此部分进行评论,并可以使用您的代码进行分页,因为您希望显示您的页码等(如果您不想使用下一个和上一页按钮等)

答案 1 :(得分:0)

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

   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>