I was attempting to build my own ‘Category Exclusion’ plugin, when it dawned on me: “why not just add a few lines into the core file?” (there are several reasons why you do not want to mess with core files, so have caution)
So, i began the search for the right file and came to the conclusion that what I was attempting to edit was wp_list_categories(). First stop, wp-includes. Searched widgets.php, nothing. Searched category.php, again nothing. Came to category-template, and resulted that wp_list_categories() is located in wp-includes/category-template.php, bam!
So, having read the 400 lines previous to what I needed, which is quite simple really, it resulted in finding line 417 and editing it accordingly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | function wp_list_categories( $args = '' ) { $defaults = array( 'show_option_all' = '', 'show_option_none' = __('No categories'), 'orderby' = 'name', 'order' = 'ASC', 'show_last_update' = 0, 'style' = 'list', 'show_count' = 0, 'hide_empty' = 1, 'use_desc_for_title' = 1, 'child_of' = 0, 'feed' = '', 'feed_type' = '', 'feed_image' = '', 'exclude' = array(3,1), /* this is the edited section, which was 'exclude' = '', */ 'exclude_tree' = '', 'current_category' = 0, 'hierarchical' = true, 'title_li' = __( 'Categories' ), 'echo' = 1, 'depth' = 0, 'taxonomy' = 'category' ); |
It was actually line 425 that needed to be edited. In my case in order to exclude the categories I needed to add multiple arguments to the parameter of ‘exclude’.
So, step by step:
- Step 1. Find /wp-includes/category-template.php and open it up in your favorite text editor
- Step 2. Find line 425
- Step 3. Find your category ID of the category you want to Hide/Remove from the widget.
- Step 4. Edit the part after the “=>” sign and delete the two apostrophes there. For one category you can just add the ID number, for more than one you will need to add the array(1,2,3,4) function – like in the example above.
- Step 5. Save Changes and upload via FTP.
Simple, Clean, and done in no time! You just want to ensure that you write down all the changes you have made to the core files so that you are prepared when your client calls you and says “something is not right here, I updated WordPress like it told me to, and now it shows all the categories, HELP!?!”. This way you know where to go and what to do…
However, it will definitely be easier to simply install the plugin that extends the wp_list_categories parameters so that any re-writes done to the core files will not tamper with the exclusions. So, I will continue to write it and leave the update here soon.
