Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>

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

 ?>

 <?php else : 
 echo 'No post published in:'.$cat->name;                
 endif; 
 wp_reset_query();
 endforeach; 

 ?>
  <?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; ?>
share|improve this question
    
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 '12 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 '12 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 '12 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 '12 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 '12 at 12:43
add comment

1 Answer

up vote 0 down vote accepted

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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