Here is a little trick to to remove categories from the main loop. Yes, you can display these elsewhere; in pages (view custom template), the sidebar, the footer… wherever you want them and I’ll show you how.
Complete code
<?php
$themes = $wpdb->get_var("SELECT term_id FROM wp_terms WHERE slug = 'themes'");
$plugins = $wpdb->get_var("SELECT term_id FROM wp_terms WHERE slug = 'plugins'");
$posts = query_posts($query_string . 'cat=-' . $themes . ',-' . $plugins);
if (have_posts()) : while (have_posts()) : the_post();
?>
Explanation
Line 1:
Opening PHP Statement
Lines 2 and 3:
The $themes and $plugins are the variables I’ve set up to pull the category id’s by name from the WordPress database. WHERE slug = ‘themes’ and WHERE slug = ‘plugins’.
Why did I setup those queries like that?
It’s a lot easier to remember a category by name than ID number isn’t it?
You can quickly change which category to remove from the loop and not have to look up the category id.
Line 4:
The $posts variable is internal to WordPress and the rest of the statement tells query_posts() to remove the categories by cat id (which we pulled from lines 2 and 3).
Line 5:
Our familiar loop is executed.
Line 6:
End of php statement
How to Add your Removed Categories anywhere you want them!
So now that you’ve removed some categories from displaying in your main loop… where do you want to put them?
The following code is how to display posts from a single category that we removed… I’ll show you how to add additional categories if you removed 2 or more from the main loop like I did in the previous code.
Complete Code
<?php
$themecat = $wpdb->get_var("SELECT term_id FROM wp_terms WHERE slug = 'themes'");
$get_themes = new WP_Query('cat=' .$themecat. '&showposts=10&orderby=post_date&order=desc');
while ($get_themes->have_posts()) : $get_themes->the_post();
$do_not_duplicate = $post->ID; ?>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php the_content();?>
<?php endwhile; ?>
Explanation
Line 1:
Opening PHP statement
Line 2:
The $themecat variable retrieves the category id WHERE slug = ‘themes’. One of the categories I removed in the above code.
Line 3:
The $get_themes variable initiates our variables for posting, here I have it setup to show 10 posts ordered in DESC fashion by date.
Line 4:
While we have posts with the $get_themes ID then start our loop!
Line 5:
Tells $get_themes not to show duplicate posts (very important).
Line 6 and 7:
You can basically put anything in here that you want that relates to the inner workings of the loop. I personally decided to just list the title and the content for illustration purposes.
Line 8:
Ending our while loop.
In order to put multiple categories (that you’ve removed from the main loop in the first portion of this tutorial) you must duplicate line 2, and change $themecat and ‘themes’ to $categorynamecat and ‘category’.
Then within line 3 replace .$themecat. with:
.$themecat. ‘,’ .$categorynamecat.
And you should now have 2 categories removed from the main loop and displayed anywhere else you’d like!
If you need any help or guidance please let me know via comments!
Popularity: 90% [?]