Is there any way to display the title of the parent nav item in the sub-menu li list Wordpress generates?
As it is wordpress spits out for a nav button labeled "Radio"
<ul class="sub-menu">
<li id="menu-item-34" class="menu-item menu-item-type-post_type menu-item-object- page menu-item-34"><a href="#">Satellite</a></li>
<li id="menu-item-33" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-33"><a href="#">Online</a></li>
<li id="menu-item-35" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-35"><a href="#">Terrestial</a></li>
</ul>
Is there any way to have it generate :
<ul class="sub-menu">
<h2> Radio</h2>
<li id="menu-item-34" class="menu-item menu-item-type-post_type menu-item-object- page menu-item-34"><a href="#">Satellite</a></li>
<li id="menu-item-33" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-33"><a href="#">Online</a></li>
<li id="menu-item-35" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-35"><a href="#">Terrestial</a></li>
</ul>
Luis Abarca answers:
With a custom walker and the start_lvl method
function start_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul class=\"sub-menu\">\n";
$output .= "\n$indent<h2>Radio</h2>\n";
}
Luis Abarca comments:
The class
class MyWalker_NavMenu extends Walker_Nav_Menu {
function start_lvl(&$output, $depth)
{
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul class=\"sub-menu\">\n";
$output .= "\n$indent<h2>Radio</h2>\n";
}
//...
}
<?php
$args = array(
'menu' => 'Main Menu',
'container' => false,
'depth' => 1,
'walker' => new MyWalker_NavMenu
);
wp_nav_menu($args);
?>
Arnav Joy answers:
How you are generating menu with the help of standard <?php wp_nav_menu( $args ); ?>
function ?
Fahad Murtaza answers:
what code is being used for the first menu generation, so that it can modified for your needs?
rizaljohn answers:
You can do that using jQuery. Something like this:
jQuery(".sub-menu menu-item").prepend("<h2> Radio</h2>");
Francisco Javier Carazo Gil answers:
Hi Shiffy,
You need use a walker:
class 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 ) ? '<span>'.esc_attr( $item->description ).'</span>' : '';
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 );
}
}
And indicate it in the function call:
wp_nav_menu( array(
'container' =>false,
'menu_class' => 'nav',
'echo' => true,
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'depth' => 0,
'walker' => new description_walker())
);
Albert Shala answers:
I found this snippet to be the easiest and cleanest way of getting better looking output for menus.
// Cleanup WP Menu html
add_filter('nav_menu_css_class', 'css_attributes_filter', 100, 1);
add_filter('nav_menu_item_id', 'css_attributes_filter', 100, 1);
add_filter('page_css_class', 'css_attributes_filter', 100, 1);
function css_attributes_filter($var) {
return is_array($var) ? array_intersect($var, array('current-menu-item')) : '';
}