WordPress – Exclude a category from feed #WordPress #WebDevelopment #Blog
Friday 28th August 2009 by adminOne problem many people have is excluding a specific category from their home page. Here is a function that does just that.
function exclude_category($query){
$your_category_id = 'the category_id to exclude
if($query->is_home)
{
$query->set('cat','-'.$your_category_id .'');
}
return $query;
}
Now to get it to work call a WordPress add_filter
add_filter('pre_get_posts','exclude_category');
and complete the function like this:
function exclude_category($query){
$your_category_id = 'the category_id to exclude
if($query->is_home)
{
$query->set('cat','-'.$your_category_id.'');
}
return $query;
}
add_filter('pre_get_posts','exclude_category');
Place the function in your relevant functions page and hey-presto $your_category_id will not appear on the home page.
Another varient to
if($query->is_home)
is
if($query->is_feed)
This applies if the page is a feed page and you have say a static home page
Popularity: 1% [?]



0
COMMENTS