Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Loop through all nav menu items checking whether the functionality has been enabled or not for them.
- * If enabled, add in submenu items for all of their descendants (only one level deep).
- *
- * @since 1.0.0
- *
- * @param array $items Array of nav menu items.
- *
- * @return array Potentially modified array of nav menu items.
- */
- public function add_children_to_menu( $items ) {
- $menu_order = count( $items ) + 1000;
- $filter_added = false;
- foreach ( $items as $item ) {
- if ( ! $this->is_menu_item_supported( $item->db_id, $item->type, $item->object ) || ! $this->is_enabled_for_menu_item( $item->db_id ) ) {
- continue;
- }
- // Get only immediate descendants (children)
- switch ( $item->type ) {
- case 'post_type':
- // Using get_pages() with 'depth' set to 1 to get only immediate children
- $children = get_pages( array(
- 'child_of' => $item->object_id,
- 'post_type' => $item->object,
- 'sort_column' => 'menu_order, post_title',
- 'depth' => 1, // Limit to one level deep
- ) );
- $parent_field = 'post_parent';
- break;
- case 'taxonomy' :
- // Using get_terms() with 'parent' set to $item->object_id to get only immediate children
- $children = get_terms( $item->object, array(
- 'parent' => $item->object_id,
- ) );
- $parent_field = 'parent';
- break;
- }
- if ( empty( $children ) || is_wp_error( $children ) ) {
- continue;
- }
- // Menu items are being added, so later fix the "current" values for highlighting
- if ( ! $filter_added ) {
- add_filter( 'wp_nav_menu_objects', array( &$this, 'fix_menu_current_item' ) );
- }
- // Add each child to the menu
- foreach ( $children as $child ) {
- $child = wp_setup_nav_menu_item( $child );
- $child->db_id = $child->ID;
- if ( empty( $child->status ) ) {
- $child->status = 'publish';
- }
- $this->added[ $child->ID ] = true; // We'll need this later
- // Set the parent menu item.
- $child->menu_item_parent = $item->ID; // Children
- // The menu_order has to be unique, so make up new ones
- // The items are already sorted due to the get_pages() or get_terms()
- $menu_order ++;
- $child->menu_order = $menu_order;
- $items[] = $child;
- }
- }
- return $items;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement