According to the docs of wp_nav_menu
we can specify our own walker function. They suggest to copy the default one and modify it as needed. But to be honest, it is a bit too complicated for the to understand.
Could you please provide a minimal example of a custom menu walker?
Say, without sub menu items, without wrapping container, without all the classes applied by the default walker. Only one class, the .current-menu-item
is important, of course. Also a way to specify the HTML element of menu items may be included.
You could create a very simple walker, like this one. To detect the current item, inspect
$item->current
. It isTRUE
for the current item only.I wouldnât add a class, but deactivate the useless link completely, because it doesnât have to be clickable anyway.
Example:
In your theme use the walker like this:
See my post about
items_wrap
for an explanation of<p>%3$s</p>
.Most time you only need to hook into the
Walker::walk()
method, for such cases, aCallback_Walker
can do it which allows you to register a callback for it.If you make use of closures and a modern PHP version (>= 5.4) you can get some of the benefits of
Closure::bind()
:This alone does nothing so far, but blatantly copying the example by @toscho, this is how it could be used:
I hope he may excuse 😉