Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_filter( 'wp_get_nav_menu_items', 'add_child_pages', 10, 3 );
- function add_child_pages( $items, $menu, $args ) {
- $menu_order = count( $items ) + 1000;
- foreach ( $items as $item ) {
- // Skip menu items that aren't top level or aren't post_type=page
- if ( 0 != $item->menu_item_parent || 'page' != $item->object )
- continue;
- // Get children pages
- $children = get_posts( array(
- 'supress_filters' => false, // Caching, etc.
- 'post_parent' => $item->object_id,
- 'post_type' => 'page',
- 'orderby' => 'menu_order',
- 'nopaging' => true,
- ) );
- // Add each child page as a child menu item to the current menu item
- // We'll worry about proper heirarchy (grandchildren) later -- this is a test
- foreach ( $children as $child ) {
- $child = wp_setup_nav_menu_item( $child );
- $child->menu_item_parent = $item->db_id; // Child of the current menu item
- // The menu_order has to be unique, so make up new ones
- // The items are already sorted anyway due to the get_posts()
- $menu_order++;
- $child->menu_order = $menu_order;
- $items[] = $child;
- }
- }
- //var_dump( $items );
- return $items;
- }
Add Comment
Please, Sign In to add comment