0

Updated: I have 50 categories and i wants to show each 5 category on one page and next 5 on next page and next 5 on next page. and so on. with pagination.

I have 50 categories in a WordPress site. I wants to show five categories on every page with pagination with 10 post. I have tried but pagination is not creating.

<?php 
    // get all the categories from the database
    $cats = get_categories();
    // loop through the categries
    foreach ($cats as $cat) {
    // setup the cateogory ID
    $cat_id= $cat->term_id;
    // Make a header for the cateogry
    ?>
        <div class="cats-by-2">
            <div class="cat-posts-left">
                <h2 class="feat-title"><span><a href=""><?php echo '<h2>'.$cat->name.'</h2>'; ?></a></span></h2>
                    <div class="posts-by-3">
                <?php
                // create a custom wordpress query
                query_posts("cat=$cat_id");
                $count = 1;

                if (have_posts()) : while (have_posts()) : the_post(); ?>

                    <div class="post-wrap<?php if ($count%3 == 0) { ?> right<?php } ?>">

                        <div <?php post_class(); ?> id="post-main-<?php the_ID(); ?>">

                        <div class="entry clearfix">

                            <h2 class="post-title"><a href="<?php the_permalink() ?>" rel="<?php _e("bookmark", "solostream"); ?>" title="<?php _e("Permanent Link to", "solostream"); ?> <?php the_title(); ?>"><?php the_title(); ?></a></h2>
                        </div>
                        <?php include (TEMPLATEPATH . "/postinfo.php"); ?>
                    </div>
                </div>
            <?php if ( $count%3 == 0 ) { ?>
                <div class="post-clear"></div>
<?php } ?>

                    <?php $count = $count + 1 ?>
                <?php endwhile; endif;
                // done our wordpress loop. Will start again for each category ?>

            </div></div>

        </div>
<?php } // done the foreach statement ?>
2
  • It is really not clear what you are trying to do. Please explain in human terms exactly what the result should be.
    – s_ha_dum
    May 30, 2013 at 14:43
  • I have 50 categories and i wants to show each 5 category on one page and next 5 on next page and next 5 on next page. and so on. with pagination
    – Johan
    May 31, 2013 at 5:55

1 Answer 1

0

First, don't use query_posts.

  1. There are no pagination functions that I can spot in your code. That is a big problem if you want pagination to work.
  2. Nor do you have a pagination parameter in your query_posts (which you should not be using) arguments.
  3. The simple pagination functions like next_posts_link may not
    work in your case anyway, without complicated filtering, as those rely on the main query and you are paginating something other than that query.

Plus, I don't see how you are even attempting to break these into fives (%3 is not going to do that ??).

I don't really see how that code is even an attempt to do what you describe, but here is an outline to get you started in the right direction. I have not made any effort to duplicate the markup you need. You should be able to work that into the Loop yourself.

// set pagination
$page = (!empty($_GET['catp'])) ? $_GET['catp'] : 1; 
// pull catagories
$cats = get_categories();
// break them into blocks of 5  
$cats = array_chunk($cats,5); 
// grab the five category IDs we need for the query 
$ids = wp_list_pluck($cats[$page - 1],'term_id');
// query for the posts in those categories
$args = array(
  'category__in' => $ids,
  'ignore_sticky_posts' => true
);
$incats = new WP_Query($args);
// Minimal Loop; proof of concept only
if ($incats->have_posts()) {
  while ($incats->have_posts()) {
    $incats->the_post();
    the_title();
    echo '<br/>';
  }
}
// paginate
$page_args = array(
    'base'         => '%_%',
    'format'       => '?catp=%#%',
    'total'        => count($cats),
    'current'      => $page,
    'show_all'     => False,
    'end_size'     => 1,
    'mid_size'     => 2,
    'prev_next'    => True,
    'prev_text'    => __('« Previous'),
    'next_text'    => __('Next »'),
    'type'         => 'plain',
);
echo paginate_links($page_args);
2
  • thnx. i is very help full to me. I have tried. but category is repeating in pagination. and also prev navigation is not working fine.
    – Johan
    Jun 1, 2013 at 10:01
  • You've done something wrong or your theme or a plugin is breaking it. That code works. I have tested it-- WordPress 3.5.1 and Twenty Twelve. Please try it exactly as I posted it.
    – s_ha_dum
    Jun 1, 2013 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.