Advertisement
Guest User

Untitled

a guest
Jan 8th, 2013
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. add_filter( 'wp_nav_menu_objects', 'submenu_limit', 10, 2 );
  2.  
  3. function submenu_limit( $items, $args ) {
  4.  
  5. if ( empty($args->menu_item_start) )
  6. return $items;
  7.  
  8. $parent_ids = wp_filter_object_list( $items, array( 'menu_item_parent' => 0 ), 'and', 'ID' );
  9.  
  10. if( !empty( $parent_ids ) ){
  11.  
  12. $menu_item_end = ( isset( $args->menu_item_end ) ) ? (int) $args->menu_item_end : count( $parent_ids );
  13. $total = ($menu_item_end) - ((int) $args->menu_item_start);
  14. $range_parent_ids = array_slice( (array) $parent_ids, ($args->menu_item_start)-1, $total+1, true );
  15.  
  16. if( !empty( $range_parent_ids ) ) {
  17. $return_items = array();
  18. foreach ( $range_parent_ids as $parent_id ) {
  19.  
  20. $return_items[] = $parent_id;
  21. $children = submenu_get_children_ids( $parent_id, $items );
  22.  
  23. if( !empty($children) ) {
  24. $return_items = array_merge( $return_items, $children );
  25. }
  26.  
  27. }
  28.  
  29. foreach( $items as $key => $item ){
  30. if( !in_array( $item->ID, $return_items ) ){
  31. unset( $items[$key] );
  32. }
  33. }
  34.  
  35. } // if !empty($range_parent_ids)
  36. } // if !empty($parent_ids)
  37.  
  38. return $items;
  39. }
  40.  
  41. function submenu_get_children_ids( $id, $items ) {
  42.  
  43. $ids = wp_filter_object_list( $items, array( 'menu_item_parent' => $id ), 'and', 'ID' );
  44. foreach ( $ids as $id ) {
  45. $ids = array_merge( $ids, submenu_get_children_ids( $id, $items ) );
  46. }
  47.  
  48. return $ids;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement