WordPress.org

Ready to get started?Download WordPress

Forums

How to show the description in the menu and allow html #2 (3 posts)

  1. Richard-B
    Member
    Posted 3 years ago #

    This is a follow up to the thread How to show the description of the menu

    Here is an improved version (with thanks to dennis.winter for the original version I based this on)

    //improve menu output and allow descriptions
    class My_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        ) .'"' : '';
    
    		$item_output = $args->before;
    		$item_output .= '<a'. $attributes .'>';
    		$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
    		if (strlen($item->description)>2) {
          $item_output .= '<br /><span class="sub">' . $item->description . '</span></a>';
    } else {
         $item_output .= '</a>';
    }
    		$item_output .= $args->after;
    
    		$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    	}
    }
    
    //strip sanitizers from menu description
    remove_filter('nav_menu_description', 'strip_tags');

    The original version added a span called 'sub' to all the menu items even if they did not have a description, this new version uses

    if (strlen($item->description)>2) {
          $item_output .= '<br /><span class="sub">' . $item->description . '</span></a>';
    } else {
         $item_output .= '</a>';
    }

    to check the description string, if it is less than one character it ignores the code to add it. One character is always counted by default which is why there is a >2 there.

    As a bonus i've added

    remove_filter('nav_menu_description', 'strip_tags');

    This allows you to add html to your menu item descriptions like line breaks and divs etc. I would be very careful adding links though as they will break the menu structure.

    Hope this helps anyone who had the same issues as me!

  2. Sushi Hands
    Member
    Posted 2 years ago #

    I love you guys. I hope you know that.
    Someone out there loves you, and that someone is me.

  3. 21cdb
    Member
    Posted 2 years ago #

    Can anyone help me to enhance the above script by a funtion two add choose a custom class for the submenu and adding a <span></span> to each ul of a given submenu. Actually i'm using this function

    class UL_Class_Walker extends Walker_Nav_Menu {
      function start_lvl(&$output, $depth) {
        $indent = str_repeat("\t", $depth);
        $output .= "\n$indent<ul class=\"sub-menu level-".$depth."\"><span></span>\n";
      }
    }

    which works pretty well, but i don't know how to combine it with the function in this thread.

    Thanks in advance!

Topic Closed

This topic has been closed to new replies.

About this Topic