BloggerCamp.com header image

How to Remove any Category from the Loop

My last post, Creating Asides in Wordpress 2.3, detailed how to make asides show up in the sidebar with WordPress’s new tagging scheme.

Now the next thing that’s necessary, at least seems to be necessary for me, is to remove the Category/Tag “Asides” from the loop, so they only show up in the sidebar.

Your main loop in index.php might look something like this…

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

Replace it with the following code:

<?php
$asideid = $wpdb->get_var("SELECT term_id FROM wp_terms WHERE slug = 'asides'");
$posts = query_posts($query_string . 'cat=-' . $asideid);
if (have_posts()) : while (have_posts()) : the_post(); ?>

This code takes the category name or tag name(depending on what you need removed) finds the associated the id. It queries the posts and removes any posts matching that id then continues through the loop.

Popularity: 26% [?]

0 Comments on “How to Remove any Category from the Loop”

Leave a Comment