Title: Modify WordPress Loop to Show Posts from Only One Category
Last modified: August 19, 2016

---

# Modify WordPress Loop to Show Posts from Only One Category

 *  [altexasgirl](https://wordpress.org/support/users/altexasgirl/)
 * (@altexasgirl)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/modification-to-wordpress-loop/)
 * Hi –
 * I’m trying to modify the WordPress loop on the home page of a new website.
 * Currently, the lead section displays every post, but I’d like to only include
   posts from a featured category. The loop already uses some WPQuery calls later
   on down the page as it lists posts in a category. Specifically, I’d like the 
   ability to tell the loop to display the posts based on category number.
 * If anyone can give me some advice on what to change to make this happen, I’d 
   be most grateful!
 * Here’s the entire index template:
 *     ```
       <?php include (TEMPLATEPATH . '/header.php'); ?>
       <?php global $visiblePostIDs; $visiblePostIDs = ''; ?>
   
       	<div id="content">
   
       		<h2 class="blocktitle"><span>Figure out something to go here</span><a href="<?php echo $feedURL; ?>" class="ico-rss"><img src="<?php bloginfo('template_directory'); ?>/images/feed.png" alt="RSS: News Maker" /></a></h2>
   
       		<div id="lastpost" class="leftcol">
       			<?php if (have_posts()) : the_post() ?>
       			<?php $visiblePostIDs .= $post->ID . ","; ?>
       				<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/thumb.php?src=<?php echo get_post_meta($post->ID, "image", $single = true); ?>&w=450&h=300&zc=1&q=95" alt="<?php the_title(); ?>" id="post-image" /></a>
       				<div class="snippet leftcol">
       					<h2 class="title"><a href="<?php the_permalink(); ?>" title="<?php the_title() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
       					<div class="excerpt"><?php the_excerpt(); ?></div>
       					<p class="readmore"><a href="<?php the_permalink() ?>">Read more</a><br /><br /><br /<img src="http://www.onthebaseline.com/uploads/Live-scoring.jpg" alt="Live Tennis Scores" height="Live Tennis Scores" width="218" height="100" /></p>
       				</div>
   
       				<div id="similar" class="rightcol">
       					<h3>Breaking Tennis News</h3>
   
       				<ul class="listposts">
       <?php delicious_bookmarks('onthebaseline', 5, true, true, false, '', false, '', false, false); ?></ul<br />
   
       				</div>		
   
       			<?php endif; ?>
       		</div>	
   
       		<div id="latestposts" class="rightcol">
       			<?php
       				query_posts('showposts=4&offset=1');
       				while ( have_posts() ) : the_post();
       					$visiblePostIDs .= $post->ID . ",";
       			?>
       			<div class="snippet">
       				<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/thumb.php?src=<?php echo get_post_meta($post->ID, "image", $single = true); ?>&w=160&h=100&zc=1&q=95" alt="<?php the_title(); ?>" class="alignleft" /></a>
   
       				<h3 class="title"><a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="bookmark"><?php the_title() ?></a></h3>
       				<div class="excerpt"><?php the_excerpt(); ?></div>
       			</div><!-- .snippet -->
       			<?php endwhile ?><img src="http://www.onthebaseline.com/uploads/NewTheme/Twitter.jpg" />&nbsp;&nbsp;&nbsp;<img src="http://www.onthebaseline.com/uploads/NewTheme/Facebook.jpg" />
   
       		</div>
   
       		<?php include (TEMPLATEPATH . '/featured.php'); ?>		
   
       		<div id="featuredcats">
   
       			<?php
       				$homepageCatIDs = explode (',', $homepageCatIDs);
       				$counter = 1;
       				foreach ($homepageCatIDs as $catID) {
       			?>
       			<?php if ($counter % 2 != 0) { ?><div class="catrow"><?php } ?>
       			<div class="catblock<?php if ($counter % 2 == 0) echo ' catblockmargin' ?>">
       				<h2 class="blocktitle"><span><?php echo get_cat_name($catID); ?></span><a href="<?php echo get_category_link($catID); ?>" class="cat-more">More <?php echo get_cat_name($catID); ?> &raquo;</a></h2>	
   
       				<?php
       					$catPosts = new WP_Query();
       					$excludePostIDs = explode (',', $visiblePostIDs);
       					$catPosts->query(array('post__not_in'=>$excludePostIDs,'showposts'=>1,'cat'=>$catID));
       				?>
       				<div class="snippet">
       					<?php if ( $catPosts->have_posts() ) : $catPosts->the_post() ?>
       						<?php $visiblePostIDs .= $post->ID . ","; ?>
       						<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/thumb.php?src=<?php echo get_post_meta($post->ID, "image", $single = true); ?>&w=180&h=120&zc=1&q=95" alt="<?php the_title(); ?>" class="featuredcats-image" /></a>
   
       						<h3 class="title"><a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="bookmark"><?php the_title() ?></a></h3>
       						<div class="excerpt"><?php the_excerpt(); ?></div>
       					<?php endif; ?>
       				</div>
   
       				<?php
       					$catPosts = new WP_Query();
       					$catPosts->query(array('post__not_in'=>$excludePostIDs,'showposts'=>3,'cat'=>$catID,'offset'=>1));
       				?>
       				<ul class="listposts">
       				<?php while ( $catPosts->have_posts() ) : $catPosts->the_post() ?>
       					<?php $visiblePostIDs .= $post->ID . ","; ?>
       					<li><a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="bookmark"><?php the_title() ?></a></li>
       				<?php endwhile ?>
       				</ul>
       			</div>
       			<?php if ($counter % 2 == 0) { ?></div><?php } ?>
       			<?php $counter++; } ?>
       			<?php if ($counter % 2 == 0) { ?></div><?php } ?>
       			INSERT ADDITIONAL CONTENT HERE
       		</div>
   
       	</div><!-- #content -->
   
       <?php get_footer() ?>
       ```
   
 * And here is the specific part of the code that I want to change (it’s posted 
   above, but this is the area where I want to include the featured articles only):
 *     ```
       <div id="lastpost" class="leftcol">
       			<?php if (have_posts()) : the_post() ?>
       			<?php $visiblePostIDs .= $post->ID . ","; ?>
       				<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/thumb.php?src=<?php echo get_post_meta($post->ID, "image", $single = true); ?>&w=450&h=300&zc=1&q=95" alt="<?php the_title(); ?>" id="post-image" /></a>
       				<div class="snippet leftcol">
       					<h2 class="title"><a href="<?php the_permalink(); ?>" title="<?php the_title() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
       					<div class="excerpt"><?php the_excerpt(); ?></div>
       					<p class="readmore"><a href="<?php the_permalink() ?>">Read more</a><br /><br /><br /<img src="http://www.onthebaseline.com/uploads/Live-scoring.jpg" alt="Live Tennis Scores" height="Live Tennis Scores" width="218" height="100" /></p>
       				</div>
   
       				<div id="similar" class="rightcol">
       					<h3>Breaking Tennis News</h3>
   
       				<ul class="listposts">
       <?php delicious_bookmarks('onthebaseline', 5, true, true, false, '', false, '', false, false); ?></ul<br />
   
       				</div>		
   
       			<?php endif; ?>
       		</div>	
   
       		<div id="latestposts" class="rightcol">
       			<?php
       				query_posts('showposts=4&offset=1');
       				while ( have_posts() ) : the_post();
       					$visiblePostIDs .= $post->ID . ",";
       			?>
       			<div class="snippet">
       				<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/thumb.php?src=<?php echo get_post_meta($post->ID, "image", $single = true); ?>&w=160&h=100&zc=1&q=95" alt="<?php the_title(); ?>" class="alignleft" /></a>
   
       				<h3 class="title"><a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="bookmark"><?php the_title() ?></a></h3>
       				<div class="excerpt"><?php the_excerpt(); ?></div>
       			</div><!-- .snippet -->
       ```
   
 * Thanks so much for the help!

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

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/modification-to-wordpress-loop/#post-1341720)
 * There’s an example here to exclude a post from the loop, just change to cat=x(
   where x is your category)
    [http://codex.wordpress.org/The_Loop#Exclude_Posts_From_Some_Category](http://codex.wordpress.org/The_Loop#Exclude_Posts_From_Some_Category)
 * Related:
    * [http://www.google.com/search?q=wordpress+exclude+categories](http://www.google.com/search?q=wordpress+exclude+categories)*
   [http://codex.wordpress.org/Template_Tags/wp_list_categories](http://codex.wordpress.org/Template_Tags/wp_list_categories)*
   [http://wordpress.org/extend/plugins/advanced-category-excluder](http://wordpress.org/extend/plugins/advanced-category-excluder)*
   [http://wordpress.org/extend/plugins/simply-exclude](http://wordpress.org/extend/plugins/simply-exclude)*
   [http://wordpress.org/extend/plugins/front-page-excluded-categories](http://wordpress.org/extend/plugins/front-page-excluded-categories)*
   [http://wordpress.org/extend/plugins/category-visibility-ipeat](http://wordpress.org/extend/plugins/category-visibility-ipeat)*
   [http://www.planetmike.com/plugins/ultimate-category-excluder](http://www.planetmike.com/plugins/ultimate-category-excluder)
 *  Thread Starter [altexasgirl](https://wordpress.org/support/users/altexasgirl/)
 * (@altexasgirl)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/modification-to-wordpress-loop/#post-1341787)
 * Hi Michael,
 * Thanks so much for your help. I appreciate it.
 * I’m not very familiar with the loop, so I do have a few more questions.
 * I added the `<?php query_posts($query_string . '&cat=-3,-8'); ?>` call here:
 *     ```
       <div id="lastpost" class="leftcol">
       		 <?php query_posts($query_string . '&cat=-6,-8'); ?>
       			<?php if (have_posts()) : the_post() ?>
       			<?php $visiblePostIDs .= $post->ID . ","; ?>
       ```
   
 * Is that right?
 * Also, do I need to make any changes to this section as well:
 *     ```
       <div id="latestposts" class="rightcol">
       			<?php
       				query_posts('showposts=4&offset=1');
       				while ( have_posts() ) : the_post();
       					$visiblePostIDs .= $post->ID . ",";
       			?>
       			<div class="snippet">
       				<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/thumb.php?src=<?php echo get_post_meta($post->ID, "image", $single = true); ?>&w=160&h=100&zc=1&q=95" alt="<?php the_title(); ?>" class="alignleft" /></a>
   
       				<h3 class="title"><a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="bookmark"><?php the_title() ?></a></h3>
       				<div class="excerpt"><?php the_excerpt(); ?></div>
       			</div><!-- .snippet -->
       			<?php endwhile ?><img src="http://www.onthebaseline.com/uploads/NewTheme/Twitter.jpg" />&nbsp;&nbsp;&nbsp;<img src="http://www.onthebaseline.com/uploads/NewTheme/Facebook.jpg" />
       ```
   
 * At this point, it’s still showing items from category 6.
 * Thanks for your help!
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/modification-to-wordpress-loop/#post-1341845)
 * Well you have multiple query_posts loops there so you will need to figure which
   one you don’t want that category.
 *  Thread Starter [altexasgirl](https://wordpress.org/support/users/altexasgirl/)
 * (@altexasgirl)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/modification-to-wordpress-loop/#post-1341887)
 * Hi Michael,
 * Sorry for not being clearer — for this section, it’s more of a case of choosing
   which category I want to show. I’d like to be able to display only posts from
   one category.
 * I know it’s possible using the loop, but I’m just not knowledgeable enough to
   know how.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/modification-to-wordpress-loop/#post-1341896)
 * With the [template tag](http://codex.wordpress.org/Template_Tags), [query_posts()](http://codex.wordpress.org/Template_Tags/query_posts)(
   or [get_posts()](http://codex.wordpress.org/Template_Tags/get_posts)) you can
   use the argument **cat=x** where the **x** is the category ID of the category
   you want results.

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

The topic ‘Modify WordPress Loop to Show Posts from Only One Category’ is closed
to new replies.

## Tags

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

 * 5 replies
 * 2 participants
 * Last reply from: [MichaelH](https://wordpress.org/support/users/michaelh/)
 * Last activity: [16 years, 5 months ago](https://wordpress.org/support/topic/modification-to-wordpress-loop/#post-1341896)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
