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.
Posted on July 23, 2009 in Wordpress How-To's and has been tagged as wordpress.
3 Comments
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!
[...] Exclude a Category from the WordPress Loop [...]
Pingback by An Introduction to the WordPress Loop » Arbenting Freebies - The Product of Being Creative — March 16, 2010 @ 9:12 am
Andy, how are you you would not believe how many hours I spent looking for this info, im an amateur and a half when it comes to WordPress and after literally 8+ hours of looking for a simple solution to my problem, you had it sitting right here on your site. Thanks for the info man, take care!
Comment by Andy — April 24, 2010 @ 5:36 pm
Thank you so much – that was so easy compared to some other ways describing how to do this.
Comment by Linda — January 30, 2011 @ 5:31 am
RSS feed for comments on this post. TrackBack URL
Leave a comment