.

3 Methods To Exclude WordPress Categories In A Post

Posted by admin | Posted in Blog, Wordpress | Posted on 16-05-2009 | Views: 241

3

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.
  1. Find the following line:
    1. $d = $options[$number]['dropdown'] ? ’1′ : ’0′;
    $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.

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

    And replace it with

    1. $cat_args = array(‘orderby’ => ’name’, ’show_count’ => $c, ’hierarchical’ => $h, ’exclude’ => $exclude);
    $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.

  1. function wp_widget_categories($args, $widget_args = 1) {
  2. extract($args, EXTR_SKIP);
  3. if ( is_numeric($widget_args) )
  4. $widget_args = array( ’number’ => $widget_args );
  5. $widget_args = wp_parse_args( $widget_args, array( ’number’ => -1 ) );
  6. extract($widget_args, EXTR_SKIP);
  7. $options = get_option(‘widget_categories’);
  8. if ( !isset($options[$number]) )
  9. return;
  10. $c = $options[$number]['count'] ? ’1′ : ’0′;
  11. $h = $options[$number]['hierarchical'] ? ’1′ : ’0′;
  12. $d = $options[$number]['dropdown'] ? ’1′ : ’0′;
  13. $exclude = ’17′;
  14. $title = emptyempty($options[$number]['title']) ? __(‘Categories’) : apply_filters(‘widget_title’, $options[$number]['title']);
  15. echo $before_widget;
  16. echo $before_title . $title . $after_title;
  17. $cat_args = array(‘orderby’ => ’name’, ’show_count’ => $c, ’hierarchical’ => $h, ’exclude’ => $exclude);
  18. if ( $d ) {
  19. $cat_args['show_option_none'] = __(‘Select Category’);
  20. wp_dropdown_categories($cat_args);
  21. ?>
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.

Related Posts with Thumbnails

Popularity: 1% [?]

No Tag

Posts that you might like:

Comments (3)

hi am new to http://www.wpsauce.org , hforum looks great :D

sndrsndvz

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! I’m sure you had fun writing this article.

very well

information you write it very clean. I’m very lucky to get this information from you.

Post a comment