HOW TO EXCLUDE SPECIFIC CATEGORIES IN WORDPRESS

Wordpress LogoAs a blogger, there are instances where you do not want to showcase some particular categories on the menu bar of your WordPress blog. WordPress by default does not give you an option to chose the categories that you would like to publish (atleast till the time this blog post was written). A simple hack into the functions.php file of your theme would easily allow you to achieve this, just follow the instructions below:

  • Login in to your WordPress admin account, open your theme editor and locate the functions.php file (you can find it at Appearance >> Editor >> Theme Functions).
  • Once you have opened the file enter the following lines of code:

function exclude_category() {

if ( is_home() ) {

set_query_var(‘cat’, ‘-2′);

}

}

add_filter(‘pre_get_posts’, ‘exclude_category’);

?>

  • This will exclude the category number 2 from the menu. You just need to find out the category number of the category that you would like to exclude and update it in the code above. If there are multiple categories that you would like to exclude then just use the code –
            

function exclude_category() {

if ( is_home() ) {

                       set_query_var('cat', '-2, -5, -7');

}

}

add_filter(‘pre_get_posts’, ‘exclude_category’);

?>

  • Alternatively if you do not wish to get your hands dirty at code level you can use the “Advanced Category Excluder” plugin to do the changes required –http://wordpress.org/extend/plugins/advanced-category-excluder/

0 comments:

Post a Comment