Advertisement
Viper007Bond

Untitled

Sep 3rd, 2011
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.28 KB | None | 0 0
  1. if ( ! is_admin() )
  2.     add_filter( 'wp_get_nav_menu_items', 'viper_add_child_pages_to_menu', 10, 3 );
  3.  
  4. function viper_add_child_pages_to_menu( $items, $menu, $args ) {
  5.     $menu_order = count( $items ) + 1000;
  6.  
  7.     foreach ( $items as $item ) {
  8.         // Skip menu items that aren't top level or aren't post_type=page
  9.         if ( 0 != $item->menu_item_parent || 'page' != $item->object )
  10.             continue;
  11.  
  12.         // Get all decendent pages
  13.         $children = get_pages( array(
  14.             'child_of' => $item->object_id,
  15.             'sort_column' => 'menu_order',
  16.         ) );
  17.  
  18.         // Add each child page to the menu
  19.         foreach ( $children as $child ) {
  20.             $child = wp_setup_nav_menu_item( $child );
  21.             $child->db_id = $child->ID;
  22.  
  23.             // Set the parent menu item.
  24.             // If the current page is a child of the current menu item,
  25.             // then use that menu item's ID otherwise use the real ID
  26.             // because it's a menu item that this plugin added itself.
  27.             if ( $child->post_parent == $item->object_id )
  28.                 $child->menu_item_parent = $item->ID;
  29.             else
  30.                 $child->menu_item_parent = $child->post_parent;
  31.  
  32.             // The menu_order has to be unique, so make up new ones
  33.             // The items are already sorted anyway due to the get_posts()
  34.             $menu_order++;
  35.             $child->menu_order = $menu_order;
  36.  
  37.             $items[] = $child;
  38.         }
  39.     }
  40.  
  41.     return $items;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement