WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have been working on this code for about a week now. I have tried many things. My recent attempts today resulted in infinite loops that just made the browser keep posting the posts.

This is what I get without limiting the posts.

bad result

This is what I have now and want setting number => 3 in my get_category args. desired result

This chunk of code below is what needs to be paginated. The allcats variable is passed to the foreach loop and returns the posts needed. The args in the foreach loop gets passed to the query which would handle getting the posts that are listed under the categories of Dinner Menu, Lunch, Pasta. It's the foreach loop that I can't figure out how to re-write into a working query that I can paginate.

 //setting the paged variable trying to get pagination
 $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

 //the allcats variable gets the child categories of Dinner menu, Lunch, Pasta that are assigned to the parent category of Menu
 $allcats = get_categories(array('child_of' => get_query_var('cat'), 'number' =>    3,'order'=> 'asc', 'paged' => $paged)); 
 foreach ($allcats as $cat) :   
      $args = array(
      'category__in' => array($cat->term_id),
      'paged' => $paged

Now, in the above code, I had hoped that setting the paged variable would work. I experimented by putting in 'paged' => $paged into the args but this does not work. Now I figured that it won't work because the number => 3 that is set above in the $allcats variable is telling WordPress to only return 3 categories. Problem is that I plan on having many more than 3 categories. I only want to display 3 categories on each page. The wine list that you see in the photo above needs to be on page 2. My issue is that I cannot figure out how to get the posts to show on a regular WordPress query so that the paged variable can do it's job. The only way I can get the posts the way I need them is by using the get_categories method with the foreach loop to get the child category.

I am using a plugin to get the images that are attached to the child category. Here is the rest of my code:

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

    //this bit of code gets the images attached to the category using the plugin
    $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' );

 //Display the category title of Dinner Menu, Lunch, Pasta
 echo '<h3>'.$cat->name.'</h3>';
 echo '<ul>';

 //Get all the post titles that are posted to the categories of Dinner Menu, Lunch, Pasta  
 while (have_posts()) : 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;                
  <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>
share|improve this question
This is difficult to understand, I suggest trying to clear up your question, strip out the code down to what is not working, show us your whole query in one chunk, and turn on debugging. – Wyck Oct 11 '12 at 1:09
Agree with Wyck. Really confusing, and the question title is not a good summary of a problem. Please rephrase the question to make it useful and understandable for future visitors. – toscho Oct 11 '12 at 1:45
Debugging is on, I don't get any errors. I did strip it down to what is not working. that is the first bit of code in the beginning and the rest of the query is below. I can't think of a better title. I can't get pagination working. What title would you suggest? Is that title better? and I added some more information about the first chunk of code. Is that more helpful? – Jamie Oct 11 '12 at 2:42
I agree with Wyck. I think you are trying to display sub categories on a category archive, but I'm not sure. More importantly, get_categories does not have any sort of offset parameter. So you can't get the first 3 categories in one query and then the second in another. – helgatheviking Oct 15 '12 at 2:03
I think you are right about the get_categories. I can't think or find another way to get the posts though and that is the problem. I don't think it is a category archive issue. The template WordPress is using is category.php. That is the file that I have modified into the code above. I created one main category called Menu and assigned all the other categories to Menu as the parent. Then all posts are assigned to one child category. This seems the only way to get the posts in the way the above photos show. – Jamie Oct 15 '12 at 12:59

Your Answer


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

Browse other questions tagged or ask your own question.