How to Exclude a Category from the WordPress Loop


Here’s the question that was asked:

“Basically I want to take all of the posts in the “Freelance” category and remove them from the main content so that I can link to them from a page instead of including them in the Loop. ”

Here is the code to exclude an entire category from appearing within the WordPress loop:

 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!-- If the post is in the category we want to exclude, we simply pass to the next post. -->
<?php if (in_category('3')) continue; ?>
<!-- Put the post tags here. I've used the permalink and content. -->
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>

Basically, what you’re doing with the above code is telling the loop that if it’s in category 3, just skip it.

Hopefully you’ve found this quick tutorial valuable. Contact me if you have any questions or would do it a different way. There’s 1,001 ways to do everything – that’s why I love coding.

Share and Enjoy:
  • Print
  • Digg
  • Facebook
  • Sphinn
  • Google Bookmarks
  • del.icio.us
  • Mixx
  • StumbleUpon
  • email

Posted on July 23, 2009 in Wordpress How-To's and has been tagged as .


Feedback for "How to Exclude a Category from the WordPress Loop"

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URL


Leave a comment


« »

Categories

Services I Offer

About Me

Andy Weigel

My name is Andy Weigel and I'm a web developer and designer in Pittsburgh, PA.

I focus on combining design with technology to build compelling, creative, easy-to-navigate web sites and custom web applications for organizations and businesses of all shapes and sizes. My specialty is WordPress. And most of all, I love what I do!

On Twitter