Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if ( ! is_admin() )
- add_filter( 'wp_get_nav_menu_items', 'viper_add_child_pages_to_menu', 10, 3 );
- function viper_add_child_pages_to_menu( $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 all decendent pages
- $children = get_pages( array(
- 'child_of' => $item->object_id,
- 'sort_column' => 'menu_order',
- ) );
- // Add each child page to the menu
- foreach ( $children as $child ) {
- $child = wp_setup_nav_menu_item( $child );
- $child->db_id = $child->ID;
- // Set the parent menu item.
- // If the current page is a child of the current menu item,
- // then use that menu item's ID otherwise use the real ID
- // because it's a menu item that this plugin added itself.
- if ( $child->post_parent == $item->object_id )
- $child->menu_item_parent = $item->ID;
- else
- $child->menu_item_parent = $child->post_parent;
- // 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;
- }
- }
- return $items;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement