Adding menu items dynamically using wp_nav_menu_objects

On my primary navigation, I want each top level page to have a list of the subpages. So I created code that dynamically adds menu items to the primary navigation. This works only on the first level of subpages. You can see that I commented out the code that goes down to the second level because it output those links on the main level. I don’t know why it does not work on the second level. Could it be that we cannot assign a menu item to a dynamically generated parent menu item?

It looks like:

add_filter( 'wp_nav_menu_objects', 'epc_wp_nav_menu_items', 10, 2 );
function epc_wp_nav_menu_items($items, $args) {
    if ($args->theme_location == 'primary') {
        $menu_order = 10000;
        $menu_order2 = 20000;
        global $wpdb;
        global $post;
        $post_id = 0;       
        if($post){
            $post_id = $post->ID;
        }

        foreach($items as $item){
            $pageChildren = $wpdb->get_results("SELECT post_title, ID FROM wp_posts WHERE post_parent = ".$item->object_id." AND post_status = 'publish' AND post_type='page' ORDER BY menu_order", 'OBJECT' );
            foreach($pageChildren as $child){
                $menu_order++;
                $new_item = epc_add_menu_item($post_id, $menu_order, $item, $child);
                $items[] = (object)$new_item;
                /*
                $pageChildrenSecondLevel = $wpdb->get_results("SELECT post_title, ID FROM wp_posts WHERE post_parent = ".$child->ID." AND post_status = 'publish' AND post_type='page' ORDER BY menu_order", 'OBJECT' );
                foreach($pageChildrenSecondLevel as $child2){
                    $menu_order2++;
                    $new_item2 = epc_add_menu_item($post_id, $menu_order2, $new_item, $child2);
                    $items[] = (object)$new_item2;
                }
                */
            }
        }
    }
    return $items;
}

function epc_add_menu_item($post_id, $menu_order, $item, $child){
                $new_item = new stdClass;
                $new_item->ID = $menu_order;
                $new_item->menu_item_parent = $item->ID;
                $new_item->url = get_permalink( $child->ID );
                $new_item->title = $child->post_title;
                $new_item->menu_order = $menu_order;
                $new_item->object_id = $child->ID;
                $new_item->post_parent = $item->object_id;
                $classes = 'menu-item';
                $classes .= ' menu-item-parent-' . $item->ID;
                $classes .= ' menu-item-type-post_type';
                $classes .= ' menu-item-object-page';
                if($post_id == $child->ID){
                    $classes .= ' current-menu-item';
                    $classes .= ' page-item';
                    $classes .= ' page-item-' . $child->ID ;
                    $classes .= ' current_page_item';
                }   
                $new_item->classes = $classes;
                return $new_item;
}

Related posts

3 comments

  1. You should also specify db_id field for the parent menu item:

    function epc_add_menu_item( $post_id, $menu_order, $item, $child ) {
        ...
        $new_item->db_id = $menu_order;
        ...
    }
    

    On a side note, menu item that has children should also have menu-item-has-children class.

  2. Use this custom class to display the sub menu:

    class Wp_Sublevel_Walker extends Walker_Nav_Menu {
    
        function start_lvl(&$output, $depth = 0, $args = array()) {
            $indent = str_repeat("t", $depth);
            $output .= "n$indent<ul class='submenu'>n";
        }
    
        function end_lvl(&$output, $depth = 0, $args = array()) {
            $indent = str_repeat("t", $depth);
            $output .= "$indent</ul>n";
        }
    
    }
    
  3. If your custom menu entries are based on posts, categories or other WP objects you can use wp_setup_nav_menu_item(WP_object) to get a menu item object.

    $item=wp_setup_nav_menu_item(get_post());
    

    That object will get most of the information set correctly, but not all information is present. We still need to do set some of the properties on the item.
    We need to set the parent menu item, if you want to make it at the top level give it the value 0.

    $item->menu_item_parent = $parentitem->ID;
    

    In order for an item to have children, you need to assign a db_id, but this NEEDS to be the ID of the WP object, it will not work (better I did not get it to work) with any other value. So we need to set that:

    $item->db_id = get_the_ID();
    

    If your current entry will have children you add menu-has-children to the classes variable. Note I also add other (css)classes required for my theme.

    $item->classes = array("menu-item", "menu-item-type-post_type", "menu-item-object-page", "menu-item-has-children");
    

    You may be able to create your own custom element by creating the item as I described above and than modifying the $item->title and $item->url but keeping the IDs. Just make sure that the ID is unique in the menu level (I did not check if it needs to be unique in the menu).

    (For others like me who found this thread while trying to create a custom menu: The menu order needs to be such that the parent is directly followed by its children.)

Comments are closed.