Advertisement
izziebytes

Wordpress - Add Descendants As Submenu Items: Remove Grandchildren

Jul 27th, 2023
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.69 KB | Source Code | 0 0
  1. /**
  2.  * Loop through all nav menu items checking whether the functionality has been enabled or not for them.
  3.  * If enabled, add in submenu items for all of their descendants (only one level deep).
  4.  *
  5.  * @since 1.0.0
  6.  *
  7.  * @param array $items Array of nav menu items.
  8.  *
  9.  * @return array Potentially modified array of nav menu items.
  10.  */
  11. public function add_children_to_menu( $items ) {
  12.     $menu_order   = count( $items ) + 1000;
  13.     $filter_added = false;
  14.  
  15.     foreach ( $items as $item ) {
  16.         if ( ! $this->is_menu_item_supported( $item->db_id, $item->type, $item->object ) || ! $this->is_enabled_for_menu_item( $item->db_id ) ) {
  17.             continue;
  18.         }
  19.  
  20.         // Get only immediate descendants (children)
  21.         switch ( $item->type ) {
  22.  
  23.             case 'post_type':
  24.                 // Using get_pages() with 'depth' set to 1 to get only immediate children
  25.                 $children = get_pages( array(
  26.                     'child_of'    => $item->object_id,
  27.                     'post_type'   => $item->object,
  28.                     'sort_column' => 'menu_order, post_title',
  29.                     'depth'       => 1, // Limit to one level deep
  30.                 ) );
  31.  
  32.                 $parent_field = 'post_parent';
  33.                 break;
  34.  
  35.             case 'taxonomy' :
  36.                 // Using get_terms() with 'parent' set to $item->object_id to get only immediate children
  37.                 $children = get_terms( $item->object, array(
  38.                     'parent' => $item->object_id,
  39.                 ) );
  40.  
  41.                 $parent_field = 'parent';
  42.                 break;
  43.         }
  44.  
  45.         if ( empty( $children ) || is_wp_error( $children ) ) {
  46.             continue;
  47.         }
  48.  
  49.         // Menu items are being added, so later fix the "current" values for highlighting
  50.         if ( ! $filter_added ) {
  51.             add_filter( 'wp_nav_menu_objects', array( &$this, 'fix_menu_current_item' ) );
  52.         }
  53.  
  54.         // Add each child to the menu
  55.         foreach ( $children as $child ) {
  56.             $child        = wp_setup_nav_menu_item( $child );
  57.             $child->db_id = $child->ID;
  58.  
  59.             if ( empty( $child->status ) ) {
  60.                 $child->status = 'publish';
  61.             }
  62.  
  63.             $this->added[ $child->ID ] = true; // We'll need this later
  64.  
  65.             // Set the parent menu item.
  66.             $child->menu_item_parent = $item->ID; // Children
  67.  
  68.             // The menu_order has to be unique, so make up new ones
  69.             // The items are already sorted due to the get_pages() or get_terms()
  70.             $menu_order ++;
  71.             $child->menu_order = $menu_order;
  72.  
  73.             $items[] = $child;
  74.         }
  75.     }
  76.  
  77.     return $items;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement