• Categories

  • Archives


    « Dynamic Meta Code for Wordpress | Main | Link building is key to longevity »

    Exclude Category from the main page or RSS feed in Wordpress

    By Henrie Media Inc. | August 6, 2008

    Have you ever wanted to exclude a category from the main page or the RSS feed of your Wordpress blog? This is the easiest way I have found to do this, without installing a plug-in.

    Put the following code anywhere in your theme’s template functions.php file. For example, if your theme is called “travel” then you would probably find this file here - wp-content/themes/travel/functions.php. If for some reason this file doe not exisit, just create it and save it in your theme’s folder.

    Copy and paste the following into the functions.php file -

    function exclude_category($query) {
    if ( $query->is_feed || $query->is_home ) {
    $query->set(’cat’, ‘-1′);
    }
    return $query;
    }
    add_filter(’pre_get_posts’, ‘exclude_category’);

    The code above excludes the category with the id of “1″ from both the RSS feed and from the main page of your blog. If you want to exclude multiple categories, simply add a comma ( , ) and a dash ( - ) followed by the category ID. For example: ‘-2, -3, -4′. You can locate the category id by logging into the Wordpress dashboard, clicking on manage, then categories.

    If you just want to exclude a category from the main page and not the RSS feed, simply replace “( $query->is_feed || $query->is_home )” with “( $query->$query->is_home )” or to remove it from just the RSS feed “( $query->is_feed)”.

    That’s all there is to it! Happy coding!

    Print This Post Print This Post

    Topics: Technical Support, Web Applications |

    Submit to Digg Submit to del.icio.us Submit to Stumpleupon Submit to Mixx Submit to Shinn

    Comments