Title: Multiple Loops
Last modified: August 19, 2016

---

# Multiple Loops

 *  [irondavy](https://wordpress.org/support/users/irondavy/)
 * (@irondavy)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/multiple-loops-3/)
 * I am trying to set up my theme so that a category page will have all the posts
   in that category running down the left half of the page. On the right half, another
   list of the posts in that category, but only ones that are tagged ‘feature’.
 * I’ve tried following guides for multiple loops but they don’t appear to work 
   properly. This is what I have now in sidebar.php:
 *     ```
       <?php rewind_posts(); ?>
   
       <?php query_posts($query_string . '&tag=feature'); ?>
   
       <?php while (have_posts()) : the_post(); ?>
   
       	<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
       		<?php the_title(); ?>
       	</a>
   
       <?php endwhile; ?>
       ```
   
 * This **does** spit out the featured posts, but not from the category I’m viewing.
   You can see what’s happening here:
 * [http://www.ihatetourists.com/category/albums/](http://www.ihatetourists.com/category/albums/)
 * “Hello world!” is in the ‘News’ category, but its showing up in the ‘Albums’ 
   category page.
 * Any help greatly appreciated!

Viewing 10 replies - 1 through 10 (of 10 total)

 *  [Maxaud](https://wordpress.org/support/users/maxaud/)
 * (@maxaud)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/multiple-loops-3/#post-680011)
 * I’m not good at the writing of the code but what if you put in &category=# or
   something similar when you query the posts, where # is the category of albums?
 *  Thread Starter [irondavy](https://wordpress.org/support/users/irondavy/)
 * (@irondavy)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/multiple-loops-3/#post-680039)
 * > I’m not good at the writing of the code but what if you put in &category=# 
   > or something similar when you query the posts, where # is the category of albums?
 * Well I can’t put in a specific category number because I want whatever one is
   being passed by the URL. Unless there’s a way of passing that. I’m not really
   sure.
 *  [Maxaud](https://wordpress.org/support/users/maxaud/)
 * (@maxaud)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/multiple-loops-3/#post-680262)
 * IS there a new tag that replaced `<?php the_category_ID(echo); ?>` similiar to`
   <?php the_ID(); ?>` that could be used to add the number to &category=# ??
 *  [Maxaud](https://wordpress.org/support/users/maxaud/)
 * (@maxaud)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/multiple-loops-3/#post-680264)
 * also,
    what if you did some if/else statements to pull up the right category 
   number?
 * `<?php if (in_category('1')) continue;?>`
 * Or would something like this work?
 * using `<?php single_cat_title('prefix', 'display'); ?>` to getthe category name
   and then entering it into something like this: (at the #####)
 *     ```
       <?php $my_query = new WP_Query('category_name=#####');
         while ($my_query->have_posts()) : $my_query->the_post();
         $do_not_duplicate = $post->ID;?>
           <!-- Do stuff... -->
         <?php endwhile; ?>
           <!-- Do other stuff... -->
         <?php if (have_posts()) : while (have_posts()) : the_post();
         if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
          <!-- Do stuff... -->
         <?php endwhile; endif; ?>
       ```
   
 *  [Maxaud](https://wordpress.org/support/users/maxaud/)
 * (@maxaud)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/multiple-loops-3/#post-680265)
 * ok,
    I know that I’ve left a lot of comments so far but would this work in your
   case?:
 *     ```
       <?php
        $categoryvariable=$cat; // assign the variable as current category
        $query= 'cat=' . $categoryvariable. '&orderby=date&order=ASC'; // concatenate the query
        query_posts($query); // run the query
        ?>
       ```
   
 *  Thread Starter [irondavy](https://wordpress.org/support/users/irondavy/)
 * (@irondavy)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/multiple-loops-3/#post-680268)
 * Thanks so much for your continued support maxaud. I tried if statements like 
   this:
 *     ```
       <?php if (is_category('1')) {
       		query_posts('cat=1&tag=feature');
       	}
       	elseif (is_category('3')) {
       		query_posts('cat=3&tag=feature');
       	}
       	elseif (is_category('4')) {
       		query_posts('cat=4&tag=feature');
       	}
       	elseif (is_category('5')) {
       		query_posts('cat=5&tag=feature');
       	}
       ?>
       ```
   
 * This solution and your solution, which I tried like this…
 *     ```
       <?php
        $categoryvariable=$cat; // assign the variable as current category
        $query= 'cat=' . $categoryvariable. '&orderby=date&order=ASC'; // concatenate the query
        query_posts($query); // run the query
        ?>
       ```
   
 * …have the same results as my original code: no posts show up. Argh!
 *  [Maxaud](https://wordpress.org/support/users/maxaud/)
 * (@maxaud)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/multiple-loops-3/#post-680272)
 * Not sure what to tell you.
    Hopefully someone else can chime in.
 * Does it show up without the “tag=feature”?
 *  Thread Starter [irondavy](https://wordpress.org/support/users/irondavy/)
 * (@irondavy)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/multiple-loops-3/#post-680274)
 * The right posts show up if I query for the category, the right posts show up 
   if I query for the tag, but if I query for both nothing shows up. Thanks for 
   all your help maxaud!
 *  [Maxaud](https://wordpress.org/support/users/maxaud/)
 * (@maxaud)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/multiple-loops-3/#post-680283)
 * Hmmm, I’m not really sure where to go from here.
    Maybe try seperating onto different
   lines like:
 *     ```
       <?php
       $categoryvariable=$cat;
       $query= 'cat=" . $categoryvariable. "'
       . '&orderby=date'
       . '&order=ASC'
       . '&tag=featured'
        query_posts($query);
        ?>
       ```
   
 * I may have the double/single quotes wrong around `. $categoryvariable.`
 *  [baffleinc](https://wordpress.org/support/users/baffleinc/)
 * (@baffleinc)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/multiple-loops-3/#post-680400)
 * use a category foreach to get the category id, then add it into the query_posts()(
   add this just before the endwhile of the **main** posts:
 *     ```
       <?php
       	foreach((get_the_category()) as $category) {
       		$category_id = $category->cat_ID . '';
       		$category_name = $category->cat_name . '';
       	}
       ?>
       ```
   
 * then in your sidebar:
 *     ```
       <h2>Other posts under <?php echo $category_name; ?></h2>
       <?php
       if(have_posts()) :
       query_posts('cat='.$category_id);
       ...
       ```
   
 * OR
 *     ```
       <h2>Other posts under <?php echo $category_name; ?></h2>
       <?php
       if(have_posts()) :
       query_posts('cateogry_name='.$category_name)
       ...
       ```
   
 * That should work fine. Just make sure the first segment of code is within the
   main Loop!

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Multiple Loops’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 10 replies
 * 3 participants
 * Last reply from: [baffleinc](https://wordpress.org/support/users/baffleinc/)
 * Last activity: [17 years, 8 months ago](https://wordpress.org/support/topic/multiple-loops-3/#post-680400)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
