Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created October 30, 2011 14:55
Show Gist options
  • Save billerickson/1325991 to your computer and use it in GitHub Desktop.
Save billerickson/1325991 to your computer and use it in GitHub Desktop.
Menu With Description class
<?php
class Menu_With_Description 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;
$item_output .= '<br /><span class="sub">' . $item->description . '</span>';
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
}
@AliMD
Copy link

AliMD commented Oct 22, 2012

@ferdouskazemi350
Copy link

Hi
this code should change some lines.
when you implement this code and use description for menu or submenu this errors appear :
Notice: Trying to get property 'link_after' of non-object in C:\xampp\htdocs\wordpress\wp-content\themes\functions.php on line 1153

Notice: Trying to get property 'after' of non-object in C:\xampp\htdocs\wordpress\wp-content\themes\functions.php on line 1156

Notice: Trying to get property 'before' of non-object in C:\xampp\htdocs\wordpress\wp-content\themes\functions.php on line 1151

Notice: Trying to get property 'link_before' of non-object in C:\xampp\htdocs\wordpress\wp-content\themes\functions.php on line 1153

Notice: Trying to get property 'link_after' of non-object in C:\xampp\htdocs\wordpress\wp-content\themes\functions.php on line 1153

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment