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 query_posts('cat=-3'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!-- Display the Title as a link to the Post's permalink. -->
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?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 remove it from the query that the loop is using.

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"

3 Comments

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!