How does a minimal menu walker look like?

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.

2 s
2

You could create a very simple walker, like this one. To detect the current item, inspect $item->current. It is TRUE 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:

class One_Line_Walker extends Walker
{
    public function walk( $elements, $max_depth )
    {
        $list = array ();

        foreach ( $elements as $item )
        {
            if ( $item->current )
                $list[] = "<b title="You are here">$item->title</b>";
            else
                $list[] = "<a href="https://wordpress.stackexchange.com/questions/112442/$item->url">$item->title</a>";
        }

        return join( "\n", $list );
    }
}

In your theme use the walker like this:

wp_nav_menu(
    array (
        'theme_location'  => 'your_theme_location',
        'walker'          => new One_Line_Walker,
        'container'       => '',
        'items_wrap' => '<p>%3$s</p>',
        'depth'           => 1
    )
);

See my post about items_wrap for an explanation of <p>%3$s</p>.

Leave a Comment