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.



Viewing 15 replies - 1 through 15 (of 53 total)
  • I’m facing exactly the same problem, and didn’t find any satisfactory solution. Played with the Walker_Nav_Menu and the filter wp_nav_menu_items with no luck.

    All I found at the moment is a hack using css, but I’m still looking for a nicer solution.

    ul.sister-pages li { display:none; }
    ul.sister-pages li.current-menu-ancestor,
    ul.sister-pages li.current-menu-ancestor li { display:block; }
    Thread Starter feggbert


    Wouldn’t it be possible to use a pgrep_replace? I have no idea how to set the RegExp…

    I think I got it. I created a custom walker for this, that displays only the menu items that have the classes “current-menu-item”, “current-menu-parent”, “current-menu-ancestor”, and all their descendants. It works fine for me, but isn’t fully tested yet.

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

    I’ve been a bit to quick, end_el and end_lvl should be overidden as well in Custom_Walker_Nav_Sub_Menu :

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

    Thread Starter feggbert


    Dude, you’re the man! Seems to work nicely for my menu, except for menuitems without sub-hierarchy there is an error

    Warning: in_array() []: Wrong datatype for second argument in /wp-content/themes/aoc/functions.php on line 80

    Which refers to this line of code:

    // Checks if the current element is in the current selection
          if (strpos($class_names, 'current-menu-item')
          || strpos($class_names, 'current-menu-parent')
          || strpos($class_names, 'current-menu-ancestor')
          || in_array( $item->menu_item_parent, $found_parents ) ) {

    This also occurs in a few more lines further down all using $found_parents…

    Thanks for your great help! (Too bad this functionality is not implemented in wp_nav_menu from the beginning…)


    oops, you’re true, change the condition with :

    if (strpos($class_names, 'current-menu-item')
        || strpos($class_names, 'current-menu-parent')
        || strpos($class_names, 'current-menu-ancestor')
        || (is_array($found_parents) && in_array( $item->menu_item_parent, $found_parents )) ) {

    Then, in end_el function, change the condition with :

    if ( is_array($found_parents) && in_array( $item->ID, $found_parents ) ) {

    Should be fine then…



    It is maybe not exactly the same problem but i solved this with jQuery. Just hide all sub-menus and then show the sub-menu if the parent happens to be .current_page_item.

    $(".current_page_item .sub-menu").show();

    Thread Starter feggbert


    Well thanks again for the nice snippet. It works nicely! Maybe you can tell me one more thing: How do I get rid of the parent item in the generated list…

    Right now the list hierarchy is like this:

    <a> Parent </a>
    <li> Child 1 </li>
    <li> Child 2 </li>
    <li> Child 3 </li>

    So basically I don’t need the <a> Parent </a> or even the surrounding <ul> and

    Any idea? Still can’t believe this function isn’t built in from the beginning…

    Thanks a lot!




    I think sushicodeur’s is the way to go.
    Here are two custom walkers, one to extract the current sub-menu, the other to omit it

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



    I have been working on this for days now…thank you so much for figuring this out!

    I second feggbert though…is there a way to get rid of/hide the top level parent link?

    Thread Starter feggbert


    johnny, at the moment i just hid it through css…not pretty but works for my client…



    I’ve had some much trouble with this. I was very surprised it wasn’t included in the wp_nav_menu function.

    The custom walker petersom3000 posted works great, except it’s only limited to a navigation depth of 2 (which I can work with, but if it was able to just filter the sub menu of the current parent, regardless of depth, it would rock!).

    Also, I too wish to hide the parent link as mentioned above. Can the custom walker be modified to support this? …or what CSS are you guys using to hide it?

    Is it also possible for someone to create a walker that lets you exclude pages/categories based on their ID (I’m having trouble doing this).




    That custom walker is great however it’s problematic when it’s returning the parent element. I’m not good enough at PHP at this point to fix that but it seems trivial enough. If you know how to do it please include that at least as an argument. Thanks a lot! This is kind of silly that it isn’t included in wp_nav_menu to begin with.

    I’ve come up with a workaround that is a little janky but it works for me, you can try and maybe it will work for you.

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

    Basically it tells the wp_nav_menu to return a string instead of echo. Next it plugs that into an XML tree. After that it counts how many
    <li> elements there are. If there are more than 1 (more than the parent menu), it will grab the child <ul> element and return that as XML. It works great for me but ideally this would be handled by wp_nav_menu natively.

    i just sort of fixed @elBranfords problem inside the walker. don’t know how “nice” this fix is, but it works for now with my site. the fix is inside the start_el function, so there is no change in anything else.

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

    it checks if the current item has the same id as the parent item and than skips it. i set the parent id to 0 (no parent id), so it only works with two level navigations. maybe someone finds a way to find the proper parent id (i didn’t really look for one). then it would work for navigations with more then two levels.

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