繁体   English   中英

分页类别列表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>

为了获得完全控制,如果“下一个”和“上一个”按钮起作用,那么您可以注释该部分,并可以使用分页代码来显示页码等(如果您不想使用“下一个”和“上一个”按钮等)

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

   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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM