3 Methods To Exclude Wordpress Categories In A Post

Sometimes, we may want to exclude some categories from display in our main page or sidebar. There are a lot of methods to exclude these categories. Here i would like to share 3 types of different method to do so.

Use Wordpress Category Visibility Plugin

This plugin will let you to exclude any tag and category at different pages. For example, you can exclude selected category in the front page, search results, archives, and feeds. Simply activate the plugin and go to your Manage->Category Visibility to manage them. (There are some users who claim this plugin may has conflict with other Wordpress plugins.)

wp_list_categories() template tags

You may use this template tags to list out all categories at your website’s navigation bar or your sidebar. The template tags tutorial website actually teach us how to include or exclude categories by adding parameters when we call this template tags.

Hack the default widgets.php

I read this tutorial from Simple Solution For smart People website. You need to make a backup of wp-includes/widgets.php file before proceed.

  1. After you backup the file. Open it using any text-editor and try to look for function wp_widget_categories. I am using Wordpress 2.6.x and it is around line 703. This is the only function that you need to modify.
  2. Find the following line:
    $d = $options[$number]['dropdown'] ? '1' : '0';

    Add $exclude = ‘17′; below this line. You may replace 17 with the Category ID that you want to exclude. You may exclude more than one category by adding a “,” between the number.

  3. Next, look for
    $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h);

    And replace it with

    $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h, 'exclude' => $exclude);

Done. You can change the “17″ to any category ID that you want to exclude anytime. I had pasted my codes at the end of this tutorial, in case you don’t understand my tutorial.

function wp_widget_categories($args, $widget_args = 1) {
    extract($args, EXTR_SKIP);
    if ( is_numeric($widget_args) )
        $widget_args = array( 'number' => $widget_args );
    $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
    extract($widget_args, EXTR_SKIP);

    $options = get_option('widget_categories');
    if ( !isset($options[$number]) )
        return;

    $c = $options[$number]['count'] ? '1' : '0';
    $h = $options[$number]['hierarchical'] ? '1' : '0';
    $d = $options[$number]['dropdown'] ? '1' : '0';
    $exclude = '17';

    $title = empty($options[$number]['title']) ? __('Categories') : apply_filters('widget_title', $options[$number]['title']);

    echo $before_widget;
    echo $before_title . $title . $after_title;

    $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h, 'exclude' => $exclude);

    if ( $d ) {
        $cat_args['show_option_none'] = __('Select Category');
        wp_dropdown_categories($cat_args);
?>

Hope you enjoying reading my tutorial. Please choose any method that suit you.