Dynamically add sub menu items

I have been able to add menu items with this statement, this however creates a flat menu. The menu has been created already. I just need to add items.

    wp_update_nav_menu_item(4, 0, array('menu-item-title' =  __($post_title), 'menu-item-url' = home_url('/'.$url.'/'), 'menu-item-status' = 'publish'));

this is super simple to use. How do I add a sub-menu item?

Topic menus Wordpress

Category Web


I have found what I was looking for the function returns an id of the menu item needed.

    $my_menu_id = wp_update_nav_menu_item(4, 0, array('menu-item-title' => __($my_menu_title), 'menu-item-url' => home_url('/'.$url.'/'), 'menu-item-parent-id' => $my_menu_id, 'menu-item-status' => 'publish')); 

by knowing the menu id you can then create sub menus attached to the first menu item using the menu-item-parent-id

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.