Most of the webmasters or blog authors who use WordPress as blogging platform displays the categories as a main navigation menu. But if you have many different categories then it may not be good to display all of them. You may want to exclude few of the categories from the list of displayed categories.
WordPress provides “wp_list_categories” method to list all the categories. To exclude certain categories from the result you can specify certain parameters to exclude. In fact you can specify the list of WordPress categories to exclude or the list of categories to include as parameters or arguments of the method.
If you are familiar with PHP then you can easily understand how this particular method is implemented. If you are not then you don’t have to worry it is simple and follow the instruction mentioned below.
How To Exclude/Include Certain Categories Using wp_list_categories method
wp_list_categories method displays list of Categories as links. Usually the category navigation menu is added in header.php file of your theme. If you open your header.php file under your theme folder you can see the usage of this particular method. For example you may find something similar to the below code in your header.php if you are using categories as your navigation menu.
1 2 3 4 5 6 | <div id="navbar"> <ul class="horizontal"> <li><a href="<?php echo get_option('home'); ?>/">Home</a></li> <?php wp_list_categories('title_li=&depth=4&orderby=name'); ?> </ul> </div> |
In the above code you can see the usage of wp_list_categories method. The normal syntax of wp_list_categories is given below.
1 | <?php wp_list_categories( $args ); ?> |
Where $args is the list of arguments the method accepts. The method accepts 25 different values as arguments. In our case we are interested in excluding certain number of categories. There are two arguments that can be used to achieve the same.
- “exclude” : Exclude one or more categories from the list of categories results. This parameter takes a comma-separated list of categories by unique ID, in ascending order.
- “include” : Only include the categories detailed in a comma-separated list by unique ID, in ascending order.
But how do you get the categories ID? Login to your blog’s wordpress admin dashboard. Click the Categories link hover your mouse over a category. The category ID will show up in the link.
Once you identified all the categories that you want to exclude from the categories menu. You can add them as part of the argument as below.
1 | <?php wp_list_categories('orderby=name&show_count=1&exclude=10,34,67'); ?> |
In the above example the wp_list_categories method will exclude the categories with ids 10,34, and 67 from the result.
Alternately you can include all the categories that you want to appear in the menu. You just need to change he parameter to include all the required category Ids. See below.
1 | <?php wp_list_categories('orderby=name&include=3,5,9,16'); ?> |
In the above example only categories with ids 3,5,9 and 16 will be available in the navigation menu.
Once you edited your header.php to exclude (or to include) certain categories as mentioned above, you can upload the file to your server using FTP tools like FieZilla. Refresh the page and you will be able to see the changes. If you are using any wordpress cache plugins like W3TotalCache etc, you may not be able to see the change immediately. You may need to clear the cache if you want to see the changes immediately.
Before you go, subscribe to get latest technology articles right in your mailbox!.