Shad Gagnon
Shad Gagnon

Reputation: 513

Wordpress 3.5 - How to only show the sub menu of a parent with wp_nav_menu?

I want to find the easiest way to get the child list of a specific page (stored inside Appearances > Menus tab) with wp_nav_menu function.

Example, I have this menu, called "primary_navigation" :

Home
Services
- Web
- Design
- Mobile
Contact

EXAMPLE OF CODE

<?php 
wp_nav_menu( array(
'theme_location' => 'primary_navigation',
'container'       => '',
'items_wrap' => '%3$s')
); 
?>

How I can get only the items inside of Services tab, example :

EXAMPLE OF CODE I SEARCHING FOR...

<?php 
    wp_nav_menu( array(
    'theme_location' => 'primary_navigation',
    'container'       => '',
    'child_of' => 'PARENT_ID'
    'items_wrap' => '%3$s')
    ); 
    ?>

I want to use wp_nav_menu function because it's easier for the client to manage navigation.

Thanks!


UPDATE :
I found this link : https://wordpress.stackexchange.com/questions/2802/display-a-portion-branch-of-the-menu-tree-using-wp-nav-menu/2809#2809

It work, but not completly and I found it a little bit complicated. With this solution, I can't get childs elements by parent ID and it seem to have problem with quote in the name.

If you have better approach, I'm really interested! :)

Upvotes: 3

Views: 9609

Answers (1)

Matt Keys
Matt Keys

Reputation: 439

I believe I have a much better solution, in your example you would be specifying the parent_id that you are looking for. I wrote a plugin which enables you to simply specify the start_depth that you want. So if you wanted to show a secondary menu, ignoring the first level menu items, you would give a start_depth of 1 like this:

wp_nav_plus(array('theme_location' => 'primary_navigation', 'start_depth' => 1));

Of course you will need to install and activate the plugin before you can start using WP Nav Plus. It is available at my website for anyone who is interested: https://mattkeys.me/products/wp-nav-plus/

As a side note: This functionality is native to many other content management systems I've used, I wonder why it is not a default option in Wordpress? I got so sick of all the workarounds and their particular 'quirks' that I finally wrote my own solution.

Upvotes: 2

Related Questions