Add Menu items description via Custom Walker for wp_nav_menu()?

Here is a solution I’ve found, and it works to append a description after the link.

My question is: Is it also possible to append a description before the link?

function add_description_to_menu($item_output, $item, $depth, $args) {
if (strlen($item->description) > 0 ) {
    // append description after link
    $item_output .= sprintf('<span class="description">%s</span>', esc_html($item->description));

    // insert description as last item *in* link ($input_output ends with "</a>{$args->after}")
    //$item_output = substr($item_output, 0, -strlen("</a>{$args->after}")) . sprintf('<span class="description">%s</span >', esc_html($item->description)) . "</a>{$args->after}";
}

return $item_output; }
add_filter('walker_nav_menu_start_el', 'add_description_to_menu', 10, 4);

Solutions Collecting From Web of "Add Menu items description via Custom Walker for wp_nav_menu()?"

I have used custom nav walker for showing descriptions in one of my project. i am sharing the code here so it might help you to solve your issue.

class annframe_description_walker extends Walker_Nav_Menu
{
      function start_el(&$output, $item, $depth, $args)
      {
           global $wp_query;
           $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

           $class_names = $value = '';

           $classes = empty( $item->classes ) ? array() : (array) $item->classes;

           $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
           $class_names = ' class="'. esc_attr( $class_names ) . '"';

           $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';

           $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
           $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
           $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
           $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';

           $prepend = '<strong>';
           $append = '</strong>';
           $description  = ! empty( $item->description ) ? '<small>'.esc_attr( $item->description ).'</small>' : '';

           if($depth != 0)
           {
                     $description = $append = $prepend = "";
           }

            $item_output = $args->before;
            $item_output .= '<a'. $attributes .'>';
            $item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
            $item_output .= $description.$args->link_after;
            $item_output .= '</a>';
            $item_output .= $args->after;

            $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
            }
}

Is it also possible to append a description before the link?

you can resort and replace the codes in above walker class. just try and see if it works…..