Title: Using query_posts to show different categories
Last modified: August 19, 2016

---

# Using query_posts to show different categories

 *  Resolved [Lindsey](https://wordpress.org/support/users/lalindsey/)
 * (@lalindsey)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/using-query_posts-to-show-different-categories/)
 * Hello!
 * I’m trying to use query_posts to split up the main page of my site (home.php)
   in the following way – I want to show 2 posts from one category, then show 1 
   post from a separate category, then show 6 posts from the same category I showed
   first.
 * So, basically I want to show 2 posts from category 1, then 1 post from category
   2, then show 6 posts from category 1, all on the same page.
 * Is query_posts the easiest way to go about this? Would this be an appropriate
   way to use it?
 *     ```
       <?php global $query_string;
       			query_posts($query_string . "&order=ASC");
       			?>
   
                    <?php query_posts('category_name=cat1&posts_per_page=2'); ?>
   
       			  <?php while (have_posts()) : the_post(); ?>
                       <!-- Do special_cat stuff... -->
                     <?php endwhile;?>
   
                     <?php $my_query = new WP_Query('category_name=cat2&posts_per_page=1'); ?>
   
       			<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
                     <!-- Do special_cat stuff... -->
                   <?php endwhile; ?>
   
                   <?php $my_query2 = new WP_Query('category_name=cat&posts_per_page=6'); ?>
   
       			<?php while ($my_query2->have_posts()) : $my_query->the_post(); ?>
                     <!-- Do special_cat stuff... -->
                   <?php endwhile; ?>
       ```
   

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

 *  Thread Starter [Lindsey](https://wordpress.org/support/users/lalindsey/)
 * (@lalindsey)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/using-query_posts-to-show-different-categories/#post-1717268)
 * Ok, this is the code I ended up using, but when I try to activate the theme, 
   I’m getting an error saying there is an error on line 18 of home.php
 *     ```
       <?php query_posts($query_string); ?>
   
                    <?php query_posts('category_name=theme&posts_per_page=2'); ?>
   
       			  <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                       <div class="theme">
                       	<a href="<?php the_permalink(); ?>"><span class="thumb"><?php the_post_thumbnail(); ?></span></a>
                           <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                           <p class="author"><strong>Author:</strong> <a href="<?php the_permalink(); ?>"><?php get_post_custom_values(Author Name); ?></a></p>
                           <?php the_excerpt(); ?>
                           <p class="cats"><?php the_category(', '); ?></p>
                           <p><a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_url'); ?>/images/download.gif" alt="Download <?php the_title(); ?> Now!" /></a></p>
                       </div>
                     <?php endwhile;?>
   
                     <?php query_posts($query_string); ?>
                     <?php $my_query = new WP_Query('category_name=article&posts_per_page=1'); ?>
   
       			<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
                     <div class="article">
   
       		<p class="more"><span class="float-right"><?php the_post_thumbnail('aticle-post-thumbnail'); ?></span><br /><a href="<?php the_permalink(); ?>">Read Complete Article</a></p>
       			<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
       			<?php the_excerpt(); ?>
       		</div>
                   <?php endwhile; ?>
   
                   <?php query_posts($query_string); ?>
                   <?php $my_query2 = new WP_Query('category_name=theme&posts_per_page=6&offset=2'); ?>
   
       			<?php while ($my_query2->have_posts()) : $my_query->the_post(); ?>
                     <div class="theme">
                       	<a href="<?php the_permalink(); ?>"><span class="thumb"><?php the_post_thumbnail(); ?></span></a>
                           <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                           <p class="author"><strong>Author:</strong> <a href="<?php the_permalink(); ?>"><?php get_post_custom_values(Author Name); ?></a></p>
                           <?php the_excerpt(); ?>
                           <p class="cats"><?php the_category(', '); ?></p>
                           <p><a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_url'); ?>/images/download.gif" alt="Download <?php the_title(); ?> Now!" /></a></p>
                       </div>
                   <?php endwhile; ?>
       ```
   
 * Any help would be appreciated!!
 * -Lindsey
 *  Thread Starter [Lindsey](https://wordpress.org/support/users/lalindsey/)
 * (@lalindsey)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/using-query_posts-to-show-different-categories/#post-1717295)
 * Nevermind, figured it out on my own with the following code (I’ll include in 
   case someone in the future needs to reference it) – still needs a teeny bit of
   tweaking, but otherwise is working perfectly!
 *     ```
       <?php query_posts('showposts=2&cat=8'); ?>
               <?php while (have_posts()) : the_post(); ?>
   
                       <div class="theme">
                       	<a href="<?php the_permalink(); ?>"><span class="thumb"><?php the_post_thumbnail(); ?></span></a>
                           <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                           <p class="author"><strong>Author:</strong> <a href="<?php the_permalink(); ?>"><?php get_post_custom_values('Author Name'); ?></a></p>
                           <?php the_excerpt(); ?>
                           <p class="cats"><?php the_category(', '); ?></p>
                           <p><a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_url'); ?>/images/download.gif" alt="Download <?php the_title(); ?> Now!" /></a></p>
                       </div>
                     <?php endwhile;?>
   
                     <?php
   
       $posts = get_posts('numberposts=1&category=9');
   
       foreach($posts as $post) :
   
       ?>
                     <div class="article">
   
       		<p class="more"><span class="float-right"><?php the_post_thumbnail('aticle-post-thumbnail'); ?></span><br /><a href="<?php the_permalink(); ?>">Read Complete Article</a></p>
       			<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
       			<?php the_excerpt(); ?>
       		</div>
                   <?php endforeach; ?>
   
                   <?php $theme = new WP_Query('showposts=6&cat=8&offset=2'); ?>
   
       			<?php while ($theme->have_posts()) : $theme->the_post(); ?>
                     <div class="theme">
                       	<a href="<?php the_permalink(); ?>"><span class="thumb"><?php the_post_thumbnail(); ?></span></a>
                           <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                           <p class="author"><strong>Author:</strong> <a href="<?php the_permalink(); ?>"><?php get_post_custom_values('Author Name'); ?></a></p>
                           <?php the_excerpt(); ?>
                           <p class="cats"><?php the_category(', '); ?></p>
                           <p><a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_url'); ?>/images/download.gif" alt="Download <?php the_title(); ?> Now!" /></a></p>
                       </div>
                   <?php endwhile; ?>
       ```
   

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

The topic ‘Using query_posts to show different categories’ is closed to new replies.

## Tags

 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)

 * 2 replies
 * 1 participant
 * Last reply from: [Lindsey](https://wordpress.org/support/users/lalindsey/)
 * Last activity: [15 years, 7 months ago](https://wordpress.org/support/topic/using-query_posts-to-show-different-categories/#post-1717295)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
