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: 100% [?]
on Jan 9th, 2008 at 2:44 am
Mark, good article!
I knew that this could be done, but your use of category’s name instead of category id make the whole hack very “portable”. I mean that if I develop a theme locally on a “test” wp installation than I put it online often the category id is different and this require further tweaking online…
Just a question: is it “safe” to add so many db query? doesn’t slow down the blog too much?
Stefano
on Jan 9th, 2008 at 3:06 am
@Stefano,
that was the whole point, to make it portable. As for the “too many queries”… no, it’s not too many queries, you can test this yourself by starting a default blog and uncommenting the “xx number of queries in xx seconds” format in the footer.php of the default theme.
Glad I was able to help, I’m actually writing a plugin for this right now!
on Sep 15th, 2008 at 5:02 pm
[...] Removing Several Categories from the Loop in WordPress There will be times when you want to take posts in a certain category out of the loop and show them someplace else. This tutorial shows you how. [...]
on Sep 18th, 2008 at 2:10 am
[...] Removing Several Categories from the Loop in WordPress 如何为特定的分类创建不同的Loop文章循环。 [...]
on Sep 20th, 2008 at 6:05 am
[...] Removing Several Categories from the Loop in WordPress There will be times when you want to take posts in a certain category out of the loop and show them someplace else. This tutorial shows you how. [...]
on Oct 3rd, 2008 at 2:58 am
Mark,
Wondering if you completed your WP plug in to Remove Several Categories from the Loop in WordPress and re-add them in on another page?
I’m not as handy working with the code…
on Nov 12th, 2008 at 8:44 am
[...] WordPress 目录 Removing Several Categories from the Loop in WordPress有时候,你想将一些帖子从特定目录中转出来,该教程会教你如何做Displaying [...]
on Nov 12th, 2008 at 9:16 am
[...] WordPress 自定义数据,菜单,目录,作者等内容的一些深度剖析与技巧 2.3. WordPress 目录 Removing Several Categories from the Loop in WordPress [...]
on Nov 12th, 2008 at 11:19 am
[...] WordPress 自定义数据,菜单,目录,作者等内容的一些深度剖析与技巧 2.3. WordPress 目录 Removing Several Categories from the Loop in WordPress [...]
on Nov 12th, 2008 at 7:50 pm
[...] Removing Several Categories from the Loop in WordPress There will be times when you want to take posts in a certain category out of the loop and show them someplace else. This tutorial shows you how. [...]
on Nov 12th, 2008 at 8:00 pm
[...] WordPress 自定义数据,菜单,目录,作者等内容的一些深度剖析与技巧 2.3. WordPress 目录 Removing Several Categories from the Loop in WordPress [...]
on Nov 12th, 2008 at 8:24 pm
[...] WordPress 自定义数据,菜单,目录,作者等内容的一些深度剖析与技巧 2.3. WordPress 目录 Removing Several Categories from the Loop in WordPress [...]