The whole world of menu walkers is still murky waters for me, so I'm hoping someone might be able to help me here.

I'm able to produce an auto-generated list of menu items on my page using the wp_nav_menu function in Wordpress. It currently looks like this:

    'theme_location' => 'main-menu',
    'start_in' => $post->ID,
    'container' => false,
    'items_wrap' => '<div class="page-categories"><ul>%3$s</ul></div>'

enter image description here

What I'd like to be able to do, is change the sub-menu headings of Face, Ear and Eye to read 'Related Face procedures', 'Related Ear procedures' etc.

It's important that the actual menu items themselves are still generated dynamically, and not hard-coded. Can this be done using a walker?


Here's one way to do it with the help of the menu CSS classes:

For the menu item that you want to modify the label of, add the wpse_menu class to it:

menu modifictation

This is supported with:

  * Modify the title of menu items that contain the 'wpse_menu' class.
  * We restrict this to the primary menu.
 add_filter( 'wp_nav_menu_objects', function( $items, $args )
     if( isset( $args->theme_location ) && 'primary' === $args->theme_location )
         foreach( $items as &$item )
             if( in_array( 'wpse_menu', $item->classes ) )
                 $item->title = __( ' Related ' ) . $item->title . __( ' products ' );       
     return $items;
 }, 10, 2 );
  • Whilst this does work, it also adds the text in the main navigation in the header itself. Is there anyway to only apply to the instance where I use the code shown? – Lee May 21 '15 at 8:24
  • I think that should work also, if this 'heading' is unique to the ones you want to change. But it could depend where this class is added @Lee – birgire May 21 '15 at 8:28
  • It's going to need to be a walker, as I don't want it to affect the main navigation (all the subheadings in the top navigation menu also have this applied to them). – Lee May 21 '15 at 8:29
  • But you only need to add 'wpse_menu' to the ones you want to change, and you can add more than one css class to each menu item, so I don't see that as a problem @Lee – birgire May 21 '15 at 8:30
  • Yeah, but that will still apply it to both the top navigation instance, and this instance which is further down the page. take a look at dev.bellavou.co.uk/face and you'll see (if you hover over the menu items, you'll see the text applied there too..) – Lee May 21 '15 at 8:31

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.