Adding WordPress menu item description using nav_menu_item_title()

Home / WordPress / Adding WordPress menu item description using nav_menu_item_title()

The answer to how to add description into WordPress menu items is – easy :). All you need to do is extend walker class using nav_menu_item_title() filter which came with WordPress 4.4.  So basically, all we need to do is to add $item->description where appropriate, which means inside the start_el() function which generates the menu link.

Have a look at the extended walker class, where I only added <span> element with the condition that if description does exist, <span> element along with description content will appear inside the <a> link element (for convenience, I removed all the comments from the walker class):

And this is how you would call this extension on targeted menu:

 

  • KorbinianMoser

    So what does this have to do with nav_menu_item_title filter? In fact I’m trying to do the same a lot easier using this filter, but weirdly $item is not available in my hooked funktion… ah, I missed the accepted_args! So you could just do add_filter( ‘nav_menu_item_title’, function($title, $item, $args, $depth) { return $title.'<span>’.$item->description.'</span>’; }, 999, 4);