I'm migrating a site from another CMS to Wordpress and am writing a tutorial on it alongside.

Wordpress has simple and straightforward solutions to most of the basic features from that CMS that I need to replicate. One big exception seems to be multi-level menus.

I need to display a primary menu level ("About" being the selected item here):


and, in a separate container, the secondary level:


I've done a lot of research on how to do this using wp_nav_menu(). There are workaround solutions out there, like this WP.SE answer and wp_nav_menu_extended, which is more or less the same approach in plugin form. Those solutions hack into wp_nav_menu() and filter out the elements that aren't in the current context.

These approaches did not work 100% for me: they wouldn't reliably filter out the right elements on every type of page.

Then there's Advanced Menu Widget that does exactly what I need, but I'd like not to have to use a wp_nav_menu() replacement that may not have other features it has. (Also I'm not sure how to call it from within the theme's code - I absolutely would want to hard-wire this.)

The approach I'm using right now is to display the entire menu, then filter out the elements that aren't part of the current second level using CSS - but that feels clunky, too, and I'm hesitant to recommend this in a professional tutorial.

Is there no straightforward approach to this that just augments wp_nav_menu()?

  • 1
    The "canonical" approach to this is a custom walker. That let's you change the entire tree, the output and even the sorting … and you are still in the "WordPress API". You can pass the current page's WP_Post object into the constructor for later use. See also the tag walker.
    – fuxia
    Jul 13 '17 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.