0

I have a sidebar nav menu set up to display the top level pages. I'd like to display subpages (3rd level) when a user clicks on a parent item. For example, when then user clicks 'Detective Division' the two subpages (Violent Crimes and Property Crimes) will show.

Here is my menu setup in admin:

Here is my code for calling the menu:

<div class="sidebar-nav hide-small">
  <?php wp_nav_menu(array(
  'theme_location' => 'sidebar-menu',
  'depth' => 2
  )); ?>
</div>

Is this controlled with 'depth' or do I need to use a walker?

0

No need for a custom walker - it's controlled with the depth parameter. At the moment, you're only showing 2 levels of menus - but you need to set this to 3!

This, however, will only control what is being output. As for the user clicking, hovering or otherwise to actually see the menu, you'll need to apply your own formatting (usually just CSS, although you could use JavaScript too) on the frontend in order to affect the display. WordPress doesn't handle this for you by default as its up to the theme.

  • Ah. I interpreted the question as meaning "does wp_nav_menu have a child_of parameter to show only a submenu". – Andy Macaulay-Brook Aug 12 '16 at 23:09
  • @AndyMacaulay-Brook Ohh.. I get it now. I read 'clicks' as 'when the user clicks it, show it' rather than 'when the user visits the page, I want to show a different menu'. :P – Tim Malone Aug 12 '16 at 23:42
  • Aah. I'm still not clear which of us is right :-) – Andy Macaulay-Brook Aug 12 '16 at 23:43
  • I'm wanting to show 2 levels as the default, then reveal the third level when the user clicks one of the menu items. I like the idea of hiding the third level using css/js then revealing it upon a click. Thanks again, @Tim Malone and @ Andy Macaulay-Brook. – tbain Aug 13 '16 at 1:37
0

Just to clarify - you click on Detective Division, go to that page and you want the menu to show just Violent Crimes, Propery Crimes and so on?

wp_nav_pages doesn't let you do that readily in the way that wp_list_pages does with the child_of argument.

You could look at your own walker.

On my own sites I tend to compromise and use wp_list_pages for subnav, with a plugin to give my users drag and drop ordering of all the subpages.

  • @Tim Malone and @ Andy Macaulay-Brook, thanks for the help. Maybe I should clarify... I only want to display the top two levels because the third level (subpages) is going to get really long and the menu will extend too far down the page. So I like to show those third level pages when the user clicks a second level page. I'm thinking at this point I'll have to use a walker class. – tbain Aug 12 '16 at 23:34
  • @tbain See my answer.. you don't need a walker, you just need to adjust the depth :) The clicking you'll need to handle with CSS or JS though. – Tim Malone Aug 12 '16 at 23:39
  • If you mean show all three levels then just use depth as either 2 or 3 depending on where you are in your site. If you do mean show only levels 2 and 3, not the top level, then you might find a way using wp_get_nav_menu_items and building a subnav from that. Easier than a custom walker maybe. – Andy Macaulay-Brook Aug 12 '16 at 23:39
  • 1
    ^ that's true too. I was going to say, that is how I usually do it on my sites, although I like your idea Andy of the wp_list_pages for subnav too. Either way @tbain if none of these answers properly solves it for you, you might want to edit your question to clarify what you want to do. – Tim Malone Aug 12 '16 at 23:41

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.