I am working with category.php. I have my posts returning how I want them, I am getting the child categories how I want them but now I am trying to add pagination and limit the amount of posts to 3. When I ran my test, the 4th post showed up on the page. I was expecting only 3 and the pagination at the bottom. I am not sure what is wrong. According to the codex, this should be working.

 $allcats = get_categories('child_of='.get_query_var('cat')); 
foreach ($allcats as $cat) :
$args = array(
 'category__in' => array($cat->term_id)

$customInCatQuery = new WP_Query($args); 

if ($customInCatQuery->have_posts()) : 
echo '<div class="menupageContent">';

        $terms = apply_filters( 'taxonomy-images-get-terms', '' );
        if ( ! empty( $terms ) ) {
        foreach( (array) $terms as $term ) {
                if($term->term_id == $cat->term_id) {
                  echo wp_get_attachment_image( $term->image_id, 'menu' );

echo '<h3>'.$cat->name.'</h3>';
echo '<ul>';    
while ($customInCatQuery->have_posts()) : $customInCatQuery->the_post(); ?>

<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

 echo '</ul></div><!--end menupageContent-->'; 


 <?php else : 
 echo 'No post published in:'.$cat->name;                

  <?php if ( $wp_query->max_num_pages > 1 ) : ?>
      <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'paradiso' ) ); ?></div>
      <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'paradiso' ) ); ?></div>
    <?php endif; ?>
  • where are you limiting the number of posts for the main query to 3? all I see is your additional category queries.
    – Milo
    Oct 7, 2012 at 16:27
  • I thought that this line would make it work, $customInCatQuery = new WP_Query($args); Are you saying that it won't? or that the query is wrong?
    – Jamie
    Oct 7, 2012 at 18:10
  • Okay, I understand now why the posts_per_page isn't working. Or it was working, just on the wrong loop. I need to limit the number of posts by the wp_query to 3 but I am having trouble doing that. I have edited the code above to take out the paged and posts_per_page
    – Jamie
    Oct 7, 2012 at 23:34
  • You are displaying posts for each child category. How do you want the pagination to work? I suppose that each child category should have its own pagination links.
    – liviucmg
    Oct 8, 2012 at 8:59
  • I am trying to get the pagination to work on the parent. So every post that gets posted will be set to a child category that will have the parent of menu. So menu is only big enough to display three children at a time. So it needs to be paginated. I know hoe to limit the posts on the children, just not the parent
    – Jamie
    Oct 8, 2012 at 12:43

1 Answer 1


I changed the first line of my code to this and it worked to limit the posts

 $allcats = get_categories(array('child_of' => get_query_var('cat'), 'number' => 3,'order'=> 'asc')); 

Now on to the next problem...Pagination

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.