Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_filter( 'wp_nav_menu_objects', 'submenu_limit', 10, 2 );
- function submenu_limit( $items, $args ) {
- if ( empty($args->menu_item_start) )
- return $items;
- $parent_ids = wp_filter_object_list( $items, array( 'menu_item_parent' => 0 ), 'and', 'ID' );
- if( !empty( $parent_ids ) ){
- $menu_item_end = ( isset( $args->menu_item_end ) ) ? (int) $args->menu_item_end : count( $parent_ids );
- $total = ($menu_item_end) - ((int) $args->menu_item_start);
- $range_parent_ids = array_slice( (array) $parent_ids, ($args->menu_item_start)-1, $total+1, true );
- if( !empty( $range_parent_ids ) ) {
- $return_items = array();
- foreach ( $range_parent_ids as $parent_id ) {
- $return_items[] = $parent_id;
- $children = submenu_get_children_ids( $parent_id, $items );
- if( !empty($children) ) {
- $return_items = array_merge( $return_items, $children );
- }
- }
- foreach( $items as $key => $item ){
- if( !in_array( $item->ID, $return_items ) ){
- unset( $items[$key] );
- }
- }
- } // if !empty($range_parent_ids)
- } // if !empty($parent_ids)
- return $items;
- }
- function submenu_get_children_ids( $id, $items ) {
- $ids = wp_filter_object_list( $items, array( 'menu_item_parent' => $id ), 'and', 'ID' );
- foreach ( $ids as $id ) {
- $ids = array_merge( $ids, submenu_get_children_ids( $id, $items ) );
- }
- return $ids;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement