简体   繁体   中英

WordPress Widget Categories hook

I see there is a get_the_categories hook. This doesn't seem to effect the categories output by the default WP categories widget.

function wp_cat_filter($categories) {
  var_dump($categories); // I'd like to remove a category before it's output.
} 
add_filter('get_the_categories','wpr_cat_filter');

This works great and I have an I have an object to work with here but not when it comes to the widget? I'm looking to remove a category here.

Is there a hook for the widget categories specifically and wouldn't that call the get_categories function?

I think you should be able to use the widget_categories_args filter:

This filter is used by the default WordPress Widget: Categories before it passes arguments to the wp_list_categories() function.

For example, something like this might work:

function wpr_cat_filter($args) {
  // remove category 1, 2 and 3
  $exclude = array(1, 2, 3);

  if (isset($args['exclude'] ) && !empty($args['exclude'])) {
     $exclude = array_unique(array_merge(explode(',', $args['exclude']), $exclude));
   }
   $args['exclude'] = implode(',', $exclude);
   return $args;
}  

add_filter('widget_categories_args', 'wpr_cat_filter');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM