Advertisement
arie_cristianD

remove the parent categories from categories widget

Oct 15th, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.46 KB | None | 0 0
  1. function exclude_parent_categories( $args ) {
  2.     $args['exclude'] = get_parent_categories();
  3.     return $args;
  4. }
  5.  
  6. function get_parent_categories() {
  7.     $parents    = array();
  8.     $categories = get_categories( array( 'hide_empty' => false ) );
  9.     foreach ( $categories as $category ) {
  10.         if ( $category->parent !== 0 ) {
  11.             $parents[] = $category->parent;
  12.         }
  13.     }
  14.     return implode( ',', $parents );
  15. }
  16.  
  17. add_filter( 'widget_categories_args', 'exclude_parent_categories' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement