0

I'm trying to show on the menus as assigned in WP Menu backend however how is possible to load child of parent page without hiding the rest.

Example

Main-1
Main-2
Main-3

When Clicking on Main-1 should show

Main-1
---Sub-1
---Sub-2
Main-2
Main-3

    <?php if (has_nav_menu( 'secondary' )) { 
    wp_nav_menu( array(
    'container'         => '', 
    'container_class'   => '', 
    'menu_class'        => '', 
    'menu_id'           => 'menu-header-menu', 
    'fallback_cb'       => 'wp_page_menu',
    'items_wrap'        => '<ul id="%1$s">%3$s</ul>',
    'sort_column'       => 'menu_order', 
    'theme_location'    => 'secondary' ) );
}

This is the main code I'm using

0

'depth'=>-1 will show all the levels (source^):

$depth
(integer) (optional) How many levels of the hierarchy are to be included where 0 means all. -1 displays links at any depth and arranges them in a single, flat list.
Default: 0

So your code will be:

<?php if (has_nav_menu( 'secondary' )) { 
    wp_nav_menu( array(
    'container'         => '', 
    'menu_class'        => '', 
    'menu_id'           => 'menu-header-menu', 
    'items_wrap'        => '<ul id="%1$s">%3$s</ul>',
    'depth'             => -1,
    'theme_location'    => 'secondary' ) );
}

And DRY - Don't Repeat Yourself. As the default value of fallback_cb is wp_page_menu, you don't need to repeat it. So do the container_class. And there's no such sort_column parameter in wp_nav_menu().

EDIT

You must put

register_nav_menus( array( 'secondary'=>__('Secondary Menu', 'text-domain') ) );

in your functions.php register the nav menu so that you can assign your menu to this location. Even though you create a new menu from Appearance » Menus, if you not assign it to the desired location, then you can't enable it to there.

  • thank you bro for the script but still not working. I want to show both parent and child of the current parent at the same it. Remark I'm using wordpress menu defining location and links. So what I want if I have - Main1 - Main2 - Main3 And clicked on Main 1 that should look like: - Main 1 --- Sub1 --- Sub2 - Main 2 - Main 3 Then I click on Main 2 it will be - Main 1 - Main 2 ---Sub1 ---Sub2 - Main 3 – Zaher Feb 3 '15 at 20:25
  • You assigned a Nav menu from Appearance > Menus to the Secondary menu location, is that correct? But it's not showing up menus and submenus...etc. I'm not getting you correctly. If you are not assigning any dynamic menu and want an automatic one why not you use wp_page_menu()? – Mayeenul Islam Feb 4 '15 at 1:01
  • thanks for the reply. Yes I'm assigning the menu from Appearance> Menus to the Secondary menu location. And exactly the problem it's taking the menu as been built in backend and not showing the submenus. I'm not using page menu because I don't wanna show all pages in the menu only selected ones – Zaher Feb 4 '15 at 12:17
  • have a look at the edit. – Mayeenul Islam Feb 5 '15 at 5:23

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.