0

Has anyone worked on sites where the menu hierarchies is fairly large?
Not more than 3 levels deep, but more than 40+ menu items?

If you only list first level items in the top menu without dropdowns and you want to show children in the left section of your site, how do you do that?

As far as I know, there is still no function like getChildren() of current selected menu item. My only solution so far is to use get_page_children() - but those children are from current page, not the menu hierarchy.

0

I found this filter online a while ago when I had similar issue. I wanted to display all children pages that were set in Appearances -> menus in the backend of wordpress. I only wanted to show the children of the current page, without using get_children. I would give credit to whoever wrote this beauty, but I can't find the source.

This filter adds an extra option to the function wp_nav_menu. It lets you add the argument submenu. The only flaw is that you need to use the parent title (not ID) to make the selection, but you can get that from the page title.

    add_filter( 'wp_nav_menu_objects', 'submenu_limit', 10, 2 );

    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;
}

This is how I use it in the sidebar:

<?php 

//Get parent title
$menu_title = empty($post->post_parent) ? get_the_title( $post->ID ) : get_the_title( $post->post_parent );

wp_nav_menu(array('menu' => 'main-menu','submenu' => $menu_title)); 


?>
  • 1
    Thanks Milo! If this is the correct answer, feel free to edit and point it to that answer instead. – gdaniel Jul 31 '14 at 20:23
  • That answer is 2 years old. I think it's a bit odd that WP has not addressed this issue. In fact, the entire menu system could be better. – Steven Jul 31 '14 at 21:10
  • I agree. But there are so many ways to approach menus that WP could not address all of them, so instead they do nothing. There are so many other things that have never been touched in the back-end that could be improved. – gdaniel Jul 31 '14 at 21:14
0

It's an issue with WP core that hierarchies don't display in Appearance > Menu > Product Categories tab. In that ticket you can find workarounds: https://core.trac.wordpress.org/ticket/18282

For me worked partially (displayed the hierarchy to the left) this plugin, discussed in the above ticket: https://core.trac.wordpress.org/attachment/ticket/18282/preserve-page-and-taxonomy-hierarchy.php

you can download it at the bottom of the page and upload in your "plugins" folder. Then go to WP Dashboad > Plugins > Activate

Now I can see the hierarchy in Appearance > Menu > Product Categories tab > All. Then I can "Select all" and add them my menu. However it adds the categories withput the original hierarchy that I had set in Products > Categories. Still they are not messed up as with the default state of WordPress (last version).

  • Is this still an issue after 4 years? – Steven Jan 3 at 17:39
  • Unfortunatly - yes. I use the latest WordPress version 5.0.2. Have lost 2-3 hours days ago to build on hand the hierarchy of product categories in a menu with around 200 options. Without the above manual plugin, I've lost 3 hours to build just 1/3 of it (later I deleted that). – Emil Nikolov Jan 5 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.