Excluding Categories from WordPress Category Widget

Introduction

Here you will see an example on how to exclude category from WordPress category widget. I am not going to use any plugin to exclude categories from WordPress category widget. For a simple change it is not recommended to use plugin because, using plugin your site’s overall page speed may decrease. As a side effect your site’s performance will be hampered.

WordPress provides a default category widget and using this category widget you can display categories as a list or as a dropdown.

So I will write a simple code snippets to exclude category or categories from both list and dropdown.

The code snippets which I will show here, you have to add into functions.php file. If you have child theme then you need to add to child theme’s functions.php file. If functions.php file doesn’t exist then you have to create under child theme. It is always recommended to add any change under child theme to avoid loosing of your changes in future update of your theme.

Exclude Categories

Let’s see the code snippets below:

function exclude_category_widget($args) {
	$excludes = "840, 841"; // Category ids to be excluded
	$args["exclude"] = $excludes;
	return $args;
}

add_filter("widget_categories_args","exclude_category_widget");
add_filter("widget_categories_dropdown_args","exclude_category_widget");

I have defined a function exclude_category_widget() and this function will remove the category or categories from list or dropdown.

You need to pass multiple category ids separated by comma to exclude multiple categories or if you want to exclude single category then you need to pass only single category.

I have added the above function to add_filter() and first line is used to remove categories from category list and second line removes categories from category dropdown.

Make sure you put the above code inside <?php ?> tag. The end tag ?> is not required but the start tag <?php must be present in the PHP file.

How to find category id?

Login to your WordPress site’s admin dashboard and go to Posts -> Categories. Now mouse over on a particular category name. Then you will see, for example, tag_ID=840. This tag_ID is the category id.

Thanks for reading.

1 thought on “Excluding Categories from WordPress Category Widget

Leave a Reply

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