We help IT Professionals succeed at work.

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

Brad Bansner
Brad Bansner asked
on
1,872 Views
Last Modified: 2017-04-03
This code:

$currentmenu=get_the_title($post->ID);
$args=array('menu' => 'Main Navigation', 'submenu' => $currentmenu);
wp_nav_menu($args);

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.
Comment
Watch Question

James RodgersWeb Applications Developer
CERTIFIED EXPERT

Commented:
have you tried just using post id?

$args=array('menu' => 'Main Navigation', 'submenu' => $post->ID);
Brad BansnerWeb Developer

Author

Commented:
I did try that, and it fails. Apparently you can't just pass an ID to 'submenu'.
Web Applications Developer
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Brad BansnerWeb Developer

Author

Commented:
Well, that helps actually. I made a mistake... there is some custom PHP involved, as shown at this URL:
http://www.ordinarycoder.com/wordpress-wp_nav_menu-show-a-submenu-only/

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 Developer

Author

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 Developer

Author

Commented:
I found a plugin, the script was not working.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions