WordPress Widget – Exclude some categories

I’ve often found a need for a simple replacement to the WordPress category widget that allows me to remove some of the categories. I whipped this up real quick for a client and thought others might find it handy. You can copy and paste it or download it from Google Code. Comment with any questions or suggestions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
class limited_catagories_list_widget extends WP_Widget {
        function limited_catagories_list_widget(){
                $widget_ops = array( 'classname' => 'Selective categories', 'description' => 'Show a list of Categories, with the ability to exclude categories' );
                $control_ops = array( 'id_base' => 'some-cats-widget' );
                $this->WP_Widget( 'some-cats-widget', 'Selective Catagories', $widget_ops, $control_ops );
        }
 
        function form ( $instance){
                $defaults = array( 'title' => 'Catagories', 'cats' => '' );
                $instance = wp_parse_args( (array) $instance, $defaults );
                ?>
                <p>
                        <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
                        <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
                </p>
                <p>
                        <label for="<?php echo $this->get_field_id( 'cats' ); ?>">Categories to exclude(comma separated list of Category IDs): </label>
                        <input id="<?php echo $this->get_field_id( 'cats' ); ?>" name="<?php echo $this->get_field_name( 'cats' ); ?>" value="<?php echo $instance['cats']; ?>" style="width:100%;" />
                </p>
                <?php
        }
 
        function update($new_instance, $old_instance) {
                $instance = $old_instance;
                $instance['title'] = strip_tags( $new_instance['title'] );
                $instance['cats'] = strip_tags( $new_instance['cats'] );
                return $instance;
        }
 
        function widget($args, $instance){
                extract( $args );
                $title = apply_filters('widget_title', $instance['title'] );
                $cats = $instance['cats'];
                echo $before_widget;
                if ( $title )
                        echo $before_title . $title . $after_title;
                echo "<ul>";
                wp_list_categories("exclude=$cats&title_li=");
                echo "</ul>";
                echo $after_widget;
        }
 
}
 
function register_jorbin_widget(){
                register_widget('limited_catagories_list_widget');
}
 
add_action('widgets_init', 'register_jorbin_widget');
 
?>

3 thoughts on “WordPress Widget – Exclude some categories

    1. Aaron Jorbin Post author

      I’d recommend putting it either in your theme’s functions.php file, in a plugin, or in a file that you can upload to your mu-plugins folder (so you don’t have to worry about activating it).

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *