custom walker class – bbird.me http://bbird.me WordPress quick tips Wed, 08 Aug 2018 16:39:07 +0000 en-US hourly 1 https://wordpress.org/?v=4.9.8 Adding WordPress menu item description using nav_menu_item_title() http://bbird.me/adding-wordpress-menu-item-description-using-nav_menu_item_title/ http://bbird.me/adding-wordpress-menu-item-description-using-nav_menu_item_title/#comments Mon, 28 Mar 2016 18:25:33 +0000 http://bbird.me/?p=453 The answer to how to add description into WordPress menu items is – easy :). All you need to do

Post Adding WordPress menu item description using nav_menu_item_title() je prvi puta viđen na bbird.me.

]]>
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):

class my_menu_walker extends Walker_Nav_Menu {

     function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        $indent = ( $depth ) ? str_repeat( "t", $depth ) : '';
 
        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $classes[] = 'menu-item-' . $item->ID;
 
        $args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );

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

        $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
        $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
 
        $output .= $indent . '<li' . $id . $class_names .'>';
 
        $atts = array();
        $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
        $atts['target'] = ! empty( $item->target )     ? $item->target     : '';
        $atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
        $atts['href']   = ! empty( $item->url )        ? $item->url        : '';
 
        $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
 
        $attributes = '';
        foreach ( $atts as $attr => $value ) {
            if ( ! empty( $value ) ) {
                $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
                $attributes .= ' ' . $attr . '="' . $value . '"';
            }
        }

        $title = apply_filters( 'the_title', $item->title, $item->ID );

        $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
        
        $item_output = $args->before;
        $item_output .= '<a'. $attributes .'>';
        $item_output .= $args->link_before . $title . $args->link_after;
        if ($item->description) {
            $item_output .= '<span>'.$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 );
    }
}

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

wp_nav_menu( array(
   'theme_location' => 'primary',
   'menu_class'     => 'primary-menu',
    "walker"       => new my_menu_walker()
) );

 

Post Adding WordPress menu item description using nav_menu_item_title() je prvi puta viđen na bbird.me.

]]>
http://bbird.me/adding-wordpress-menu-item-description-using-nav_menu_item_title/feed/ 1