Add Descendants As Submenu Items

When I created the navigation menu at the top of my site using the menu feature in WordPress (Appearance → Menus), I didn’t want to have to maintain anything but the top-level menu items. When I released a new plugin and created a page for it, I didn’t want to have to go into my menu UI and add it there too. So instead I wrote this plugin.

Add Descendants As Submenu Items adds a checkbox to each menu item that is of a hierarchical post type (i.e. pages) or taxonomy (i.e. categories). Checking this box will automatically display all descendants as submenu items on the front end of your site. You can see it in action at the top of my site — I only configured the top level menu items and everything that shows up when you hover over them has been added by my plugin rather than manually by me.

Download:

Screenshots:

108 thoughts on “Add Descendants As Submenu Items

  1. Alex, is it possible to specify a limit for the depth of the children? For example, I want to add only direct children to the menu item but not also children of these children.

      • The page linked above is gone but I found the code using archive.org. Here it is for others.

        Find this section:

        case ‘post_type’:
        // Using get_pages() instead of get_posts() because we want ALL descendants
        $children = get_pages( array(
        ‘child_of’ => $item->object_id,
        ‘post_type’ => $item->object,
        ‘sort_column’ => ‘menu_order, post_title’,
        ) );
        and change to:

        case ‘post_type’:
        // Using get_pages() instead of get_posts() because we want ALL descendants
        /* Edit by gbmhunter to remove grandchildren. Removed the following
        $children = get_pages( array(
        ‘child_of’ => $item->object_id,
        ‘post_type’ => $item->object,
        ‘sort_column’ => ‘menu_order, post_title’,
        ) );
        */
        /* And added this */
        $children = get_pages(‘child_of=’.$item->object_id.’&parent=’.$item->object_id);

    • For anyone who is still looking to do this, I ran the code through chat GPT and tested it to make sure it works and functions. It did a solid job adjusting. You can grab that code here: https://pastebin.com/WVJxhK4H

      Notes:
      → This will affect the entire plug-in to not include grandchildren so you’ll just get Parent -> Child and it will stop there. This will affect the entire site so you can’t choose to have some menus with grandchildren etc and some without.
      → Code snippet above is for a specific section of the plug-in and does NOT replace the entire plug-in code. Make sure you’re replacing the right section and triple check cause duplicating or missing a spot can / will break the site (happened to me once and I had to resolve by going through FTP / File manager on my host and deleting the whole plug-in folder).

      It starts with
      /**
      * Loop through all nav menu items checking…

      and ends with
      return $items;
      }

  2. Great plugin, Alex. I wasn’t sure at first whether each parent item could have submenus enabled separately, but a quick play with the plugin confirmed this feature. One less thing for the less technically-minded person who updates the site to have to worry about!

  3. Pingback: Using Categories in Navigation Menu for WordPress

  4. Hi Alex, thank you very much, you’ve saved me a ton of work. I’ve rated the plugin 5 stars at wordpress.org where there is a message ‘This plugin hasn’t been updated in over 2 years.’ etc. I have it working on the current WordPress 3.9.2 using Uber menu 3. so it is working. Perhaps you can change the date of the plugin or something to remove the message.
    I have spent many hours searching for a way to automatically add child or sub categories to a top menu item in wordpress. I believe this is the only plugin that will do that.
    thanks again
    Rob

    • Yeah, I need to go through all my plugins and bump their “tested up to” version numbers. I write my plugins well the first time around so I rarely need to update them — they just continue to work fine with new versions of WordPress. 🙂

      Glad you like the plugin!

  5. Nice works great for regular categories… Any chance i could get it to work with QWoocommerce Product Categoies? I tried and it doesnt seem to work with it

  6. Strange ya I just tried and it doesnt seem to work with my woocommerce sub menus do you think you could login mayeb and see Im in no way a WordPress Expert yet lol Im more uh CS-Cart guy but moved to woo mainly due to it having great plugin support liek YOUR! ha

  7. I have installed your plugin, however when I click Automatically add all descendants as submenu items in Appearance-Menus , I do not get all my descendant children subcategory items shown when I hover over the main menu. Do you know why?

  8. Pingback: My Favorite WordPress Plugins | cmarabate.com

  9. Pingback: My Favorite WordPress Plugins | Bonkage.com

  10. Pingback: My Favorite WordPress Plugins - Chris Marabate's BlogChris Marabate's Blog

  11. Pingback: The Best WordPress Plugins – WordPress Site Handbook

  12. Hi, what a great plugin!
    How can you hide empty subcategories, that is, they have no items in stock?
    Currently shows all subcategories associated with a category.

    Thank you.

    • That would make more sense I think, and I believe it’s the default behavior for most places in WordPress. I’ll try to take a look when I have a chance in a few weeks.

      • Hi Alex, have you ever gotten around this request please ? It would be so great if only categories with products availables would be displayed in the menu 😀 Thanks in advance for your response 😉 Bye

  13. I love this plugin, thank you so much! The only issue that I’ve found with it, is that when it is enabled, I get the following error message at the top of the customizer (appearance > customize):

    Fatal error: Uncaught Exception: Supplied nav_menu_item value missing property: status in /home/publicr2/public_html/wordpress/wp-includes/customize/class-wp-customize-nav-menu-item-setting.php:183 Stack trace: #0 /home/publicr2/public_html/wordpress/wp-includes/class-wp-customize-nav-menus.php(692): WP_Customize_Nav_Menu_Item_Setting->__construct(Object(WP_Customize_Manager), ‘nav_menu_item[2…’, Array) #1 /home/publicr2/public_html/wordpress/wp-includes/class-wp-hook.php(286): WP_Customize_Nav_Menus->customize_register(Object(WP_Customize_Manager)) #2 /home/publicr2/public_html/wordpress/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array) #3 /home/publicr2/public_html/wordpress/wp-includes/plugin.php(453): WP_Hook->do_action(Array) #4 /home/publicr2/public_html/wordpress/wp-includes/class-wp-customize-manager.php(926): do_action(‘customize_regis…’, Object(WP_Customize_Manager)) #5 /home/publicr2/public_html/wordpress/wp-includes/class-wp-hook.php(286): WP_Customize_Manager->wp_loaded(”) #6 /home/ in /home/publicr2/public_html/wordpress/wp-includes/customize/class-wp-customize-nav-menu-item-setting.php on line 183

    Any ideas what may be going on?

    Thanks very much,
    Matt

      • Exactly th same error for me. I installed the plugin on a new wordpress installation with the twentysewenteen template.
        The plugin works very well everywhere except when I open “appearance > customize”.
        Do you have found a solution?

        Thanx
        Stan

        • I haven’t found a solution, unfortunately 🙁 I just disable it temporarily if I really need to use the customize/preview functionality… only workaround that seems to work for now :\

  14. Not sure if I missed a step while configuration this plugin. The sub categories of my main categories aren’t showing. Even though I checked “Automatically add all descendants as submenu items”. Advise anyone.

  15. Would really like to use this plugin, but we need to limit the submenu to the children, and not display the grandchildren. I see someone linked to a tutorial above but the link is a dead page. Any other advice on code to add to remove grandchildren?

  16. Hi there! Sadly the Plugin does not work, when using Woocommerce and the enfold theme. The Customizer will not load, only a 500 Error will appear.
    Best wishes

  17. Thanks for this great plugin. Very simple but very useful – especially when creating submenus based on taxonomies!

    Anybody looking to fix the error 500 in the customizer:

    edit add-descendants-as-submenu-items.php, go to line 293 (after the line that reads “$child->db_id = $child->ID;”) then add this code:

    if (empty($child->status)) {
    $child->status = ‘publish’;
    }

    That fixed it for me (WordPress 5.2.3).

Comments are closed.