I'm trying to hide a large amount of categories from my category widget. I've tried a few plugins but none of them seem to want to let me use the dropdown option. I looked into the widget_categories_args hook and that seems to be what I want but I can't get it to work.
Anyways here's my code
function widget_categories_args_filter( $cat_args ) {
$exclude_arr = array( 57,61,63,56,55,62,52,53,54,67,65 );
if( isset( $cat_args['exclude'] ) && !empty( $cat_args['exclude'] ) )
$exclude_arr = array_unique( array_merge( explode( ',', $cat_args['exclude'] ), $exclude_arr ) );
$cat_args['exclude'] = implode( ',', $exclude_arr );
return $cat_args;
}
add_filter( 'widget_categories_args', 'widget_categories_args_filter', 10, 1 );
I Took that from here:
I'm putting this in my theme's functions.php. That shouldn't matter even though the function is set for plugins, right?
I'm trying to hide a large amount of categories from my category widget. I've tried a few plugins but none of them seem to want to let me use the dropdown option. I looked into the widget_categories_args hook and that seems to be what I want but I can't get it to work.
Anyways here's my code
function widget_categories_args_filter( $cat_args ) {
$exclude_arr = array( 57,61,63,56,55,62,52,53,54,67,65 );
if( isset( $cat_args['exclude'] ) && !empty( $cat_args['exclude'] ) )
$exclude_arr = array_unique( array_merge( explode( ',', $cat_args['exclude'] ), $exclude_arr ) );
$cat_args['exclude'] = implode( ',', $exclude_arr );
return $cat_args;
}
add_filter( 'widget_categories_args', 'widget_categories_args_filter', 10, 1 );
I Took that from here: https://codex.wordpress/Plugin_API/Filter_Reference/widget_categories_args
I'm putting this in my theme's functions.php. That shouldn't matter even though the function is set for plugins, right?
Share Improve this question asked Jun 19, 2015 at 19:37 FranticJ3FranticJ3 1791 gold badge3 silver badges9 bronze badges 7 | Show 2 more comments5 Answers
Reset to default 14I know this post is pretty old, but because I came across the same issue and this post came up higher than one with a solution, I figured I'd add this, which worked for me.
Source: http://coffeecupweb/how-to-exclude-or-hide-categories-from-category-widget-in-wordpress-sidebar/
//Hide categories from WordPress category widget
function exclude_widget_categories($args){
$exclude = "1,4,8,57,80";
$args["exclude"] = $exclude;
return $args;
}
add_filter("widget_categories_args","exclude_widget_categories");
This works: https://gist.github/peltopiri/76e7d1143e33b424633114103cfae5ec
<?php
function exclude_woocommerce_widget_product_categories($widget_args) {
//Insert excluded category ids here
$excludes = array(12,33);
$includes = explode(",",$widget_args['include']);
$includes = array_filter($includes, function($value) use ($excludes) {
return !in_array($value, $excludes);
});
$widget_args["include"] = implode(",", $includes);
return $widget_args;
}
add_filter( 'woocommerce_product_categories_widget_dropdown_args', 'exclude_woocommerce_widget_product_categories');
add_filter( 'woocommerce_product_categories_widget_args', 'exclude_woocommerce_widget_product_categories');
Hide the uncatecogrized / default category in WooCommerce:
I guess I'm not the only one who came to this page in search for a way to hide the default / uncategorized category introduced in WooCommerce 3.3.
If you're one of them, instead of hard-coding the category ID which may be different in different environments / installations, you can use the following snippet, modified from the snippet by Mike Jolley to hide it from the Woocommerce Product Categories widget:
<?php // Do not include this if already open!
/**
* Code goes in theme functions.php.
*
* If you use dropdown instead of hierachical view,
* hook to the following filter instead:
* `woocommerce_product_categories_widget_dropdown_args`
*/
add_filter( 'woocommerce_product_categories_widget_args', 'custom_woocommerce_product_categories_widget_args' );
function custom_woocommerce_product_categories_widget_args( $args ) {
$args['exclude'] = get_option( 'default_product_cat' );
return $args;
}
After several searches and tests, hiding post categories for list items and drop-down is done separately since they're called by different filter ids.
For list category items use
add_filter("widget_categories_args","YOUR_CUSTOM_FUNCTION");
For drop-down category items use
add_filter("widget_categories_dropdown_args","YOUR_CUSTOM_FUNCTION");
Reference: https://basicwp/exclude-categories-from-category-widgets-in-wordpress/
I was able to hide a single category using CSS:
li.cat-item-1 {
display: none;
}
I put this CSS snippet in my theme's "Additional CSS".
Category 1 is "Uncategorized", which is the one I wanted to hide.
1
. Try1000
. As I said, this works for me so there may be another function interfering. Setting the priority high might give yours the last word. Are you using the Core category widget? – s_ha_dum Commented Jun 22, 2015 at 14:25