0

I need to add custom links, on the fly, to the navigation menu. I can add custom links to the first level of items (created via Appearance > Menus), but for some reason, I cannot add a custom link, child of another custom link create previously..

Here's my code:

function on_the_fly($items) {

    $menu_items = array();

    foreach ($items as $item) {
        $item->menu_order = count($menu_items) + 1;
        $menu_items[] = $item;

        $new_menu_item = new \WP_Post((object) array(
            'ID' => $item->ID . "00",
        ));

        $new_menu_item->post_author = 1;
        $new_menu_item->comment_status = 'closed';
        $new_menu_item->ping_status = 'closed';
        $new_menu_item->post_type = 'nav_menu_item';
        $new_menu_item->post_title = 'test 1';
        $new_menu_item->post_name = 'test 1';
        $new_menu_item->filter = 'raw';
        $new_menu_item->object_id = $new_menu_item->ID;
        $new_menu_item->object = 'custom';
        $new_menu_item->type = 'custom';
        $new_menu_item->type_label = 'Custom Link';
        $new_menu_item->menu_order = count($menu_items) + 1;
        $new_menu_item->menu_item_parent = $item->ID;
        $new_menu_item->url = str_replace("//", "/", $item->url . "/" . sanitize_title('test 1'));
        $new_menu_item->title = 'test 1';

        $menu_items[] = $new_menu_item;

        $new_menu_item_2 = new \WP_Post((object) array(
            'ID' => $new_menu_item->ID . "00",
        ));

        $new_menu_item_2->post_author = 1;
        $new_menu_item_2->comment_status = 'closed';
        $new_menu_item_2->ping_status = 'closed';
        $new_menu_item_2->post_type = 'nav_menu_item';
        $new_menu_item_2->post_title = 'test 2';
        $new_menu_item_2->post_name = 'test 2';
        $new_menu_item_2->filter = 'raw';
        $new_menu_item_2->object_id = $new_menu_item->ID;
        $new_menu_item_2->object = 'custom';
        $new_menu_item_2->type = 'custom';
        $new_menu_item_2->type_label = 'Custom Link';
        $new_menu_item_2->menu_order = count($menu_items) + 1;
        $new_menu_item_2->menu_item_parent = $new_menu_item->ID;
        $new_menu_item_2->url = str_replace("//", "/", $item->url . "/" . sanitize_title('test 2'));
        $new_menu_item_2->title = 'test 2';

        $menu_items[] = $new_menu_item_2;

    }
    return $menu_items;
}

add_filter('wp_nav_menu_objects', 'on_the_fly', 1,1);

Result:

> - Application
>  - test 1
> - test 2

And not:

> - Application
>   - test 1
>     - test 2

How can I achieve the desired result?

1

There is a really good example on this page if you want to have a look and see if it helps - https://isabelcastillo.com/dynamically-sub-menu-item-wp_nav_menu

0

It seems that the missing argument was "db_id". If defined, the tree will be created as intended.

$new_menu_item->db_id = $new_menu_item->ID;

Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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