Skip to content

Instantly share code, notes, and snippets.

@iamcanadian1973
Created July 5, 2014 02:31
Show Gist options
  • Save iamcanadian1973/1ebef357616aae2db3d2 to your computer and use it in GitHub Desktop.
Save iamcanadian1973/1ebef357616aae2db3d2 to your computer and use it in GitHub Desktop.
Enable menu description
add_filter( 'walker_nav_menu_start_el', 'child_enable_menu_description', 10, 2 );
function child_enable_menu_description( $item_output, $item ) {
if ( ' ' !== $item->post_content )
return preg_replace( '/(<a.*?>[^<]*?)</', '$1' . '<span>' . $item->post_content . '</span><', $item_output );
return $item_output;
}
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 );
}
}
add_filter( 'wp_nav_menu_args' , 'wpsites_menu_descriptions' );
function wpsites_menu_descriptions( $args ) {
$args['walker'] = new Menu_Description;
$args['desc_depth'] = 0;
return $args;
}
/**
* @author Brad Dalton - WP Sites
*
* @link http://wpsites.net/web-design/theme-support-menu-item-descriptions/
*/
// old way
/*
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 .= ! empty( $item->description ) ? '<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 );
}
}
//add_action( 'wp_head', 'change_sub_nav' );
function change_sub_nav() {
//add_filter( 'theme_mod_nav_menu_locations', array( $this, 'theme_mod' ) );
add_filter( 'wp_nav_menu_args', 'modify_nav_menu_args' );
}
function modify_nav_menu_args( $args )
{
$menu_id = FALSE;
//if( $menu_id == $args['menu_id'] )
//{
$walker = new Menu_With_Description;
$args['walker'] = $walker;
//}
return $args;
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment