While reading though some twitter updates this morning, I stumbled across one that was asking if anyone knew a way to exclude a specific category in Wordpress from the loop, unless it was certian page template. After a quick minute of thinking, I came up with this small function that will do just that.
Place in your functions.php file
if( !is_page_template('user-feed.php') ){ // Replace 'user-feed.php' with your page template file name
global $wp_query;
$cat = get_cat_ID('user_feed'); // Replace 'user_feed' with your desired category to exclude from the regular loop
$wp_query->query_vars['cat'] = '-'.$cat;
}
How this works is that first it checks to see if the page or post we're viewing is NOT USING the page template that we want to category to appear in, and if it's not, adjusts the query_vars to exclude that category from the loop.
If the page the user is viewing IS USING the page template, then the query_vars are left unchanged and that category's posts are displayed in the loop.


Share the Love






Andrew Turner
July 7th
Thanks for posting that! I finally figured out a different solution after a few days, It's a little different to this one, but works much the same.
Flex Developer
July 31st
very nice article…keep on the good work