Reputation: 687
How to put this category list in a pagination because currently I have a code that show all the list of category including post by category. The problem is how to make it paginate if the category list exceed by 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 ?>
Upvotes: 0
Views: 2725
Reputation: 687
For those who are looking for answer: Here is how to paginate the category list if you are showing all categories in a page.
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('« Previous') ?></div>
<div class="alignright"><?php next_posts_link('More »') ?></div>
Upvotes: 0
Reputation: 631
You are not using $paged variable at all in your query. Replace your code section
// create a custom wordpress query
query_posts("cat=$cat_id&posts_per_page=5&depth=1");
with
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);
And it will work fine for you, additionally also you can use
<div class="alignleft"><?php previous_posts_link('« Previous') ?></div>
<div class="alignright"><?php next_posts_link('More »') ?></div>
</div>
For even full control, if Next and Previous buttons work, then you can comment this part and can use your code for pagination as you want to show your page numbers etc (if you don't want to use next and prev buttons etc)
Upvotes: 1