Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

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


?>
share|improve this answer
    
1  
Thanks Milo! If this is the correct answer, feel free to edit and point it to that answer instead. –  gdaniel Jul 31 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 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 at 21:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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