WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Nov 16 2011

How to Get All the Children of a Specific Nav Menu Item

***UPDATED (12/6/2011): I have just updated this function to allow for basic depth (all or none).

Recently, I needed to get all the children (along with the parent) of a particular nav menu item. Since pages can do this very well, I looked into the function get_page_children(). So I modified that function to create get_nav_menu_item_children(). I have recommended this to WordPress core (ticket). What are your thoughts?

[php]
/**
* Returns all child nav_menu_items under a specific parent
*
* @param int the parent nav_menu_item ID
* @param array nav_menu_items
* @param bool gives all children or direct children only
* @return array returns filtered array of nav_menu_items
*/
function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth = true ) {
$nav_menu_item_list = array();
foreach ( (array) $nav_menu_items as $nav_menu_item ) {
if ( $nav_menu_item->menu_item_parent == $parent_id ) {
$nav_menu_item_list[] = $nav_menu_item;
if ( $depth ) {
if ( $children = get_nav_menu_item_children( $nav_menu_item->ID, $nav_menu_items ) )
$nav_menu_item_list = array_merge( $nav_menu_item_list, $children );
}
}
}
return $nav_menu_item_list;
}
[/php]

Written by Travis Smith · Categorized: WordPress

StudioPress Premium WordPress Themes     WP Engine Managed WordPress Hosting

What can I do for you!?

Custom Development

We develop plugins by determining both business/functional and technical requirements, following WordPress development best practices, and using agile methodology to ensure you get the best solution.

Consulting

Have questions? Need a reliable developer to consult? Please contact us today!

Customized Theme

We can customize your theme or child theme, or create a child theme for you based on your needs while enhancing the performance of every individual attribute.

Customized Plugin

We can customize your plugins, extend plugins (e.g., Gravity Forms, Jetpack, Soliloquy) based on your needs ensuring security, performance, and positive business impact.

Contact Us

About Travis Smith

As a WordPress enthusiast, developer, and speaker, Travis writes about what he learns in WordPress trying to help other WordPress travelers, beginners and enthusiasts with tutorials, explanations, & demonstrations.

Comments

  1. chodorowicz says

    November 17, 2011 at 12:41 pm

    That would be very useful. I’ve been needing it lately and finally used Gecka Submenu plugin (but having it inbuilt or just as a function in functions.php would be much better).

    I don’t get the second parameter ‘nav_menu_items’ – could you elaborate on it a bit…

    Reply
    • Travis Smith says

      November 17, 2011 at 1:02 pm

      Hello,

      It can be populated by nav_menu_items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );

      Thanks,
      Travis

      Reply
  2. Rafael Ehlers says

    November 18, 2011 at 3:46 pm

    I think your code, is similar to this: http://www.viper007bond.com/wordpress-plugins/add-descendants-as-submenu-items/

    Reply
  3. manoj says

    June 7, 2013 at 8:20 am

    Thanks Dude

    Reply
  4. Kurt says

    August 22, 2013 at 2:46 pm

    This sounds like something I have been searching for. Everything I find is always based on post hierarchy instead of menu hierarchy. Could you give me some additional direction on how to implement this. I am fairly new to WordPress. How do I use this. Where do I put this code?

    Thank You!

    Reply
  5. Nancy says

    March 22, 2015 at 7:12 pm

    Huzzah! Easy fix for a cumbersome problem. Thanks for sharing your efforts!

    Reply
  6. Vishal Kakadiya says

    April 14, 2016 at 9:05 am

    Thank you so much man, this code save my lots of time.

    Reply
  7. Nils Harder says

    September 17, 2018 at 9:25 am

    Nice Idea, But I cannot make it work. $nav_menu_item_list is empty.

    Reply
  8. Didou Schol says

    September 8, 2020 at 5:45 am

    Excellent, this worked like a charm! Thanks for putting it out there, it helped me a lot.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2025 � WP Smith on Genesis on Genesis Framework � WordPress � Log in