• I’m trying to exclude a category from my theme’s categories sidebar widget. I know this can be done through editing the core widgets.php file, however, in my circumstance in needs to be edited through my themes folder.

    Is this possible? I haven’t been able to dig anything up as of yet.

    Thanks.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Looking at the core code looks like you can hook into the ‘widget_categories_dropdown_args’ filter to alter the arguments when the wp_dropdown_categories() function is called from the categories widget.

    The arguments for that function includes one to exclude one or more categories. Check out the wp_dropdown_categories() for more information on line 301 of ‘wp-includes/category-template.php’.

    Thread Starter thelumberjack

    (@thelumberjack)

    Thanks, Samuel. How would I go about replacing the ID number with a variable in the following code?

    function exclude_category($query) {
    	if ( $query->is_feed ) {
    		$query->set('cat', '-5');
    	}
    return $query;
    }
    
    add_filter('pre_get_posts', 'exclude_category');

    I want to replace the 5 with $variable but the normal methods don’t seem to be working?

    There shouldn’t be any problems with replacing the ‘-5’ value with $variable.

    However, I’m not sure that code will alter the categories shown in the sidebar widget. It will only affect the categories/posts in the current WordPress loop?

    If not then you will need to use something more along the lines of the filter hook I suggested.

    Thread Starter thelumberjack

    (@thelumberjack)

    Hi, dgwyer.

    I tried to change it to $query->set('cat', '-$variable'); and it failed. Any other ideas? To me it seems like a simple PHP mistake on my end – I’m not exactly a PHP expert at the moment. It’s not a problem with the variable, as I’ve echoed it and it returns a value of 8 (the category I’m trying to exclude).

    As for the hook, this theme will be available for download, so unfortunately I’m unable to edit WordPress’ core files. Thanks for your help though, much appreciated.

    You need to alter it to be something like:

    $query->set('cat', $variable);

    and make sure $variable holds the value (or values) of categories you wish to exclude.

    As for the hook, the whole point is you don’t EVER have to alter WordPress core files. You use hooks from code added to your themes functions.php file to alter the behaviour, or content outputted from WordPress core files.

    Thread Starter thelumberjack

    (@thelumberjack)

    Thanks. I worked out my variable issue with a little bit of tinkering.

    ??

    So what you’re saying is, if you were to copy over a function from the core file, edit it to your liking and paste it into your theme’s functions.php file, basically it would overwrite the default one? Or am I completely missing the point?

    No, you basically ‘subscribe’ to a hook and define a callback function to execute at a specific point in WP.

    This is a fundamental principal in WordPress that gives you amazing power over what you can do but without you having to ever touch a core WordPress file. See here for more information:

    https://codex.www.ads-software.com/Plugin_API

    Thread Starter thelumberjack

    (@thelumberjack)

    Thanks, dude. Will have a read through it all now.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Exclude Category From Categories Widget Without Editing Core Files’ is closed to new replies.