0

So this is what I'm fruitlessly trying to achieve - let's say there's a menu like the following...

enter image description here

What I'd like to do is to generate a menu with just the title and the link of the current page the user is on and it's immediate children. For example let's say the user is currently on Sample Page - the menu should look like this...

enter image description here

If the user is currently viewing the Woo page, the generated menu should contain a link to the Woo page and it's children (Shop, Cart, Checkout, My Account). If the page the user is viewing has no children like on Yet Another Page, it should only display a link to the page the user is currently on (Yet Another Page in this case).

Is something like this possible to achieve? If so, how can I do this? Any tips would be greatly appreciated and thank you in advance.

0

You have to hook into wp_nav_menu_items and build your own menu there. A little guide for working with this hook can be found at wpbeginner.

0

You can also try to use CSS only for this.

Every menu-item in a WordPress menu get several classes. For example, if a menu-item is currently active, one of the classes will be current-menu-item.

If the current active menu-item is in a submenu, the parent menu-item will also get, multiple classes, current-menu-ancestor current-menu-parent current_page_parent current_page_ancestor.

So you could try

  • display:none; to all .sub-menu elements, so that default all submenus are hidden
  • and than try to only show the submenu if the parent is active, .current-menu-parent .sub-menu > display:block;

Just take a look at all the different classes in your source code, maybe you need to use another class instead of .current-menu-parent, but you should get the idea.

  • Am I right in saying the only way to do this is via JS by cloning the element with the .current-menu-item class and hiding/removing its parent? As far as I can tell it's not possible to hide an elements parent while keeping it visible in CSS. Still, thanks for the tip - I'll give it a go. – Paweł Panysz Jun 30 '17 at 17:56

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.