Support » Fixing WordPress » wp_nav_menu: List only 2nd level (separate submenu)?

  • Hi all,

    I am currently fiddling around with the new wp_nav_menu. So far I like it. But I am having troubles building a separate vertical navigation that shows the parents sub-pages only (as definded in the Menu settings in the backend). Is this possible with this new functionality? Or do I still have to use wp_list_pages? This would be quite inconvenient, since I would have to manage another hiearchy in the pages or category settings. I’ve looked around the web for a solution but couldn’t find a fix. I guess v3.0 is yet too new.

    Thanks for a quick note.

    Regards,

    Felix

Viewing 15 replies - 16 through 30 (of 53 total)
  • Here’s my solution for 3 level navigation, based on @elbradford method:

    $menu_string = wp_nav_menu($menu_args);
    $xml = new SimpleXMLElement($menu_string);
    $i = 0;
    foreach($xml->li as $li) {
        $classes = array('current-page-ancestor','current-menu-item','current-menu-parent');
        if(strstr($li['class'],'current-page-ancestor') || strstr($li['class'],'current-menu-item') || strstr($li['class'],'current-menu-parent')) {
            $out= $i;
    };
    $i++;
    }
    
    if(!empty($out)) {echo '<ul>'. $xml->li[$out]->asXML() . '</ul>';}

    Wow this is amazing… thank you sushicodeur and everyone else for your hard work!

    One issue I have found is if the item (page/category etc) you are visiting appears in multiple trees it will list all trees in the menu.

    For example with the following custom menu…

    PARENT 1 — PARENT 2 — PARENT 3
    ITEM A —– ITEM B —– ITEM C
    ITEM D —– ITEM D —– ITEM F
    ITEM G —– ITEM H —– ITEM I

    … the walker that everyone has so kindly worked on will generate a tree showing all the children (and parents) of any parent which contains duplicate items, so in the above example, as item D appears as a child twice, a list will be generated like so…

    PARENT 1
    ITEM A
    ITEM D
    ITEM G
    PARENT 2
    ITEM B
    ITEM D
    ITEM H

    If anyone has any suggestions on how to limit the results to just one tree I would be greatly appreciative!

    This is almost exactly what I need. I’m having the same issue as the user above. Check out this link:

    http://ht.ly/2BY46

    Does anyone have an idea of how the code can be tweaked to only show the links for the section it appears in the menu?

    Before I implemented this code, I already had a function in place that listed child pages if in a parent. But, the client wants certain child pages to be in more than one category.

    Jenny Jane,

    On my browser I’m not seeing any of the menu items being highlighted. I’m assuming you have the proper CSS catching the class of the selected menu item and modifying its appearance appropriately?

    Actually, I’m not really sure what your looking to do.

    Bradford

    Did you look in the sidebar? 🙂

    You are currently on Transcripts, which in the menu can be found under both Parents and Students. Because Transcripts appears under two main nav items, it lists both sections in the sidebar.

    I’m looking for a way to only list the children that I specified under either Parents or Students.

    The more I think about it, the more it seems unlikely to be achievable. No matter what, Transcripts is only one page. How would you specify which sub-menu to display? Seems like it would have to be dependent on which link they clicked on the menu…

    Oh well!

    Thanks Guys to putting this code together. Worked for me.

    Hello,

    What about this small snippet? it works fine.. i made it. it is listing only 2nd level of menu.

    $menu = wp_nav_menu( array( 'menu_id' => 'nav-2', 'theme_location' => 'primary', 'echo'=>0 ) );
    					$menu = preg_match_all('/(<ul class="sub-menu">.*<\/ul>)(.*<\/li>)/s',$menu,$matches);
    					$menu = $matches[1][0];
    					$menu = str_replace('class="sub-menu"','id="nav-2"',$menu);
    					echo $menu;

    Nice job everyone, especially sushicodeur for the original class. Custom_Walker_Nav_Sub_Menu is PERFECT.

    Too bad it was so hard to find this thread and this functionality wasn’t included in the nav_menu function to begin with!

    Hi, I was tryin new Custom_Walker_Nav_Sub_Menu() in my menu widget “s walker input but I get the following error

    Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'new Custom_Walker_Nav_Sub_Menu()::walk' was given in /home/www/wp-includes/nav-menu-template.php on line 443

    who knows how to use the Custom_Walker_Nav_Sub_Menu from within widgets ?

    thanks

    @feggbert
    @johnnybebopcool
    @kaiey

    I wanted to hide the parent element too. I created a solution for my scenario. I have a sub menu that I list inline below the main navigation tabs. I only want the navigation listed if it’s the current tab of course. I override the WALKER class function “walk” instead of the level and element tag functions like Sushi did.

    The result is only the sub navigation and no extra
    UL tags. You use the same arguments as sushi does for the original walker he created.

    [Code moderated as per the Forum Rules. Please use the pastebin]

    Ah! Alright, that solution above stinks. I found a better solution that’s much simpler and works perfectly.

    http://www.darrenhoyt.com/2008/02/12/creating-two-tiered-conditional-navigation-in-wordpress/

    Ah! Alright, that solution above stinks. I found a better solution that’s much simpler and works perfectly.

    @just MOE

    that’s simple but it doesn’t utilize wp_nav_menu functionality…isn’t that what’s being discussed here?

    //Frankie

    Anyone know of any of the scripts here allow me to only display the sub menu of a specific parent menu? For example, if there’s 3 menu item’s, home, about, contact..

    Let’s say there’s 5 sub menu links under “about”.

    How can I use wp_nav_menu to only display the items under the “about” menu item?

    I can recommend the thread on Stack Exchange.

    The answers from Rarst works perfect.

    I have tried all the custom walker classes ive found on the net including these and havnnt had luck at getting it to successfully not display children of unactive parent items. i did come up with this function though that will return what the correct menu. Not sure if it will be of any help to anybody but here it is.

    [Code moderated as per the Forum Rules. Please use the pastebin]

Viewing 15 replies - 16 through 30 (of 53 total)
  • The topic ‘wp_nav_menu: List only 2nd level (separate submenu)?’ is closed to new replies.