ozh

adds child pages as menu items to top level menu items

ozh
Sep 3rd, 2011
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.14 KB | None | 0 0
  1. add_filter( 'wp_get_nav_menu_items', 'add_child_pages', 10, 3 );
  2.  
  3. function add_child_pages( $items, $menu, $args ) {
  4.     $menu_order = count( $items ) + 1000;
  5.  
  6.     foreach ( $items as $item ) {
  7.         // Skip menu items that aren't top level or aren't post_type=page
  8.         if ( 0 != $item->menu_item_parent || 'page' != $item->object )
  9.             continue;
  10.  
  11.         // Get children pages
  12.         $children = get_posts( array(
  13.             'supress_filters' => false, // Caching, etc.
  14.             'post_parent' => $item->object_id,
  15.             'post_type' => 'page',
  16.             'orderby' => 'menu_order',
  17.             'nopaging' => true,
  18.         ) );
  19.  
  20.         // Add each child page as a child menu item to the current menu item
  21.         // We'll worry about proper heirarchy (grandchildren) later -- this is a test
  22.         foreach ( $children as $child ) {
  23.             $child = wp_setup_nav_menu_item( $child );
  24.  
  25.             $child->menu_item_parent = $item->db_id; // Child of the current menu item
  26.  
  27.             // The menu_order has to be unique, so make up new ones
  28.             // The items are already sorted anyway due to the get_posts()
  29.             $menu_order++;
  30.             $child->menu_order = $menu_order;
  31.  
  32.             $items[] = $child;
  33.         }
  34.     }
  35.  
  36.     //var_dump( $items );
  37.  
  38.     return $items;
  39. }
Add Comment
Please, Sign In to add comment