0

I am building a nav menu programmatically like so:

$menu_exists = wp_get_nav_menu_object( 'test_menu' );
if( !$menu_exists){
    $menu_id = wp_create_nav_menu('test_menu');
    buildMenu($menuArray,$menu_id);
}

function buildMenu($array,$menu_id) {

    foreach ($array as $menu) {

        var_dump( wp_update_nav_menu_item($menu_id, 0, array(
            'menu-item-title' => $menu[0]->post_title,
            'menu-item-object' => 'page',
            'menu-item-object-id' => $menu[0]->ID,
            'menu-item-type' => 'post_type',
            'menu-item-status' => 'publish',
            'menu-item-parent-id' => $menu[0]->post_parent)));


        if(!empty($menu[1])) {
            buildMenu($menu[1],$menu_id);
        }
    }

}

Where $menuArray is collection of post objects. This is working, however all the menus items get added to the top level and the menu-item-parent-id is ignored. Does WordPress expect the menu-item-parent-id to be the nav menu id or the post id?

6
  • Can you show us the $menuArray value? At least a sample non-working $menuArray value.
    – cybmeta
    Commented Feb 9, 2015 at 13:10
  • @cybmeta its just a collection of post objects. The array its self is working fine, the only problem is the items are not nesting to their parents. Commented Feb 9, 2015 at 13:16
  • Well, I need it to look for a possible problem and its solution, if you don't provide it I can not reproduce the issue.
    – cybmeta
    Commented Feb 9, 2015 at 13:20
  • @cybmeta added example Commented Feb 9, 2015 at 14:38
  • I can not see the example and the example you added before was not useful. Anyway, I think that menu-item-parent-id needs a ID of an already menu item id.
    – cybmeta
    Commented Feb 9, 2015 at 16:13

1 Answer 1

0

As I originally suspected, menu-item-parent-id takes the id of the menu item and not the id of the parent page.

So I referenced the menu item ID and passed it into the recursive function :

function buildMenu( $array, $menu_id, $parent_id = false ) {

    foreach ($array as $menu) {

        $menuItemId = wp_update_nav_menu_item(
            $menu_id,
            0,
            array(
                'menu-item-title'     => $menu[0]->post_title,
                'menu-item-object'    => 'page',
                'menu-item-object-id' => $menu[0]->ID,
                'menu-item-type'      => 'post_type',
                'menu-item-status'    => 'publish',
                'menu-item-parent-id' => $parent_id
            )
        );


        if(!empty($menu[1])) {
            buildMenu( $menu[1], $menu_id, $menuItemId );
        }

    }

}
1
  • I've edited the question to fix some typo erros and to add $parent_id to the buildMenu function. I'd still like to see what $array is, so then we can know what is $menu[1] and know what this code exactly does.
    – cybmeta
    Commented Feb 10, 2015 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.