I would like a sub-menu in which the user visits a page, post, or category (in this example, “info”) and is then displayed a list of children categories/pages. Not a dropdown menu, but a menu that appears only when the user is already in the parent category.
I need a code which continues to display the parent’s children even when on that child page, post, or category. Maybe there is a way to do this with WP menus that I am unaware of?
The code needs to be separate from the main menu and be assigned the traditional CSS classes which indicate if the user is on the current page. The code also needs to automatically recognize the children of the parent, as opposed to assigning the children and calling multiple menus throughout the site. I’m positive this is possible because I used to utilize such a code. However, I can’t remember what that code was!
Below is an illustrated example of what such a menu may look like:
Funny, I just ran into this same conundrum a few months back…
Background
I found it quite bizarre that WordPress applies all manner of CSS classes to menu-items in order to distinguish the current item and its ancestors, but then completely ignores the sub-menus and their relationship with the current item. I wanted a
.current-sub
CSS class so that I could hide all.sub-menu
s except.current-sub
s.Theory
After much research and wading through source, the solution that I ended up embracing was to extend the
Walker_Nav_Menu
class and modify its behavior at two distinct points in order to obtain my imagined.current-sub
CSS class:Walker_Nav_Menu->start_el()
), examine the classes that WordPress assigned to the menu-item in order to determine if it corresponds to the current page or is a menu ancestor of the current page.Walker_Nav_Menu->start_lvl()
), if the previous menu-item processed was either the current menu-item or an ancestor of it, apply a new class to the sub-menu element in order to distinguish it as a current submenu.After the Walker was implemented, all I needed was a new call in a template file to
wp_nav_menu()
with an instance of my new menu Walker as an argument.Implementation
For convenience and brevity I’ve provided my implementation in a procedural-style plugin. For the most part the code is identical to the native
Walker_Nav_Menu
– my comments denote my additions.The entire contents of the referenced
style.css
file are as follows:And finally, placing a call to
wp_nav_menu()
within a template file where the menu is to be displayed in order to make use of the new Walker (see the documentation for additional arguments):Final Notes
in_array()
calls before the$classes
array is joined rather than usingstrpos()
calls afterwards. There’s also the possibility of usingarray_flip()
and subsequently usingisset( $classes[ $class_name ] )
, though I suspect that the flip would defeat any performance gained from usingisset()
.This may not be the exact solution you were looking for (and there may well exist better approaches to the issue), but I hope it’s enough to set you on the right path!
I’m using the following code on one of my websites:
Please try this out and check for your desired features. If it doesn’t (exactly) do what you want, please let me know and I will try to update the code.