Best way to add description to navigation menu

Hello guys, I am trying to add description to wp_nav_menu, I have found that there is one way to do it like this

$item_html .= ' <span class="menu-desc">' . $item->description . '</span>';

Now what I want to do is I want to add that description to same <a></a> tag and I am doing it like this

$item_description = '<span class="navbar-text visible-md visible-lg">' . $item->description . '</span>';

$item_html_new .= str_replace('</a>',$item_description.'</a>',$item_html);

$output .= $item_html_new;

It works fine but it doesn’t look very well for me, I am wondering is it a good way to do it? Or maybe you know a better way? Thanks and sorry for english mistakes if there is any !

Here’s a solution if anyone needs this in the future (for the Sage starter theme):

  1. Go to ‘lib’ -> ‘nav.php’

  2. Locate ‘function start_el(…) {’ function section.

  3. Add this IF statement inside the function:

    // Add Description to Menu Item (inside anchor tag)
    if (strlen($item->description) > 0 ) {
    $item_html = substr($item_html, 0, -strlen("{$args->after}")) . sprintf(’%s’, esc_html($item->description)) . “{$args->after}”;

Done :smile:

This post helped… Resolved.

Rejoicing at first to have found a proper / simple solution for introducing descriptions for my menu items I was baffled to see that the current Sage install (8.2.1) doesn’t contain the nav-file any more…

What to do? Does this have to do with the step towards being framework agnostic?

Looking forward to gaining some insight :pensive:

Thanks + regards,