Use wp_nav_menu in WordPress to get submenu list by menu ID number

This code:

$args=array('menu' => 'Main Navigation', 'submenu' => $currentmenu);

Open in new window

...gets the title of the current page and passes it to the wp_nav_menu function, within the Main Navigation menu, to get that page's submenu.

It works, until there are two pages with the same title. In my site, there are two pages called Services, each located under different parent menus, so the code fails.

How could the above code be modified to work based on the current page's ID number instead of page title? Or is there a better way than that?

Thank you.
Brad BansnerWeb DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

James RodgersWeb Applications DeveloperCommented:
have you tried just using post id?

$args=array('menu' => 'Main Navigation', 'submenu' => $post->ID);
Brad BansnerWeb DeveloperAuthor Commented:
I did try that, and it fails. Apparently you can't just pass an ID to 'submenu'.
James RodgersWeb Applications DeveloperCommented:
I looked online and i don't see the 'submenu' option in the codex

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Brad BansnerWeb DeveloperAuthor Commented:
Well, that helps actually. I made a mistake... there is some custom PHP involved, as shown at this URL:

I believe that is what I used. Sorry, I build this quite a while ago, and must have forgotten there was some customization involved.
Brad BansnerWeb DeveloperAuthor Commented:
So probably in addition to passing the ID, I need to modify some of this:

function submenu_limit($items, $args){
	if (empty($args->submenu)) return $items;
	$parent_id=array_pop(wp_filter_object_list($items, array('title' => $args->submenu), 'and', 'ID'));
	$children=submenu_get_children_ids($parent_id, $items);
	foreach ($items as $key => $item){
		if (!in_array($item->ID, $children)) unset($items[$key]);
	return $items;

function submenu_get_children_ids($id, $items){
	$ids=wp_filter_object_list($items, array('menu_item_parent' => $id), 'and', 'ID');
	foreach ($ids as $id){
		$ids=array_merge($ids, submenu_get_children_ids($id, $items));
	return $ids;

Open in new window

Brad BansnerWeb DeveloperAuthor Commented:
I found a plugin, the script was not working.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.