Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Adding Title to Wordpress Sub-Nav Menu? WordPress

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>

Answers (7)

2012-01-10

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);
?>

2012-01-10

Arnav Joy answers:

How you are generating menu with the help of standard <?php wp_nav_menu( $args ); ?>
function ?

2012-01-10

Fahad Murtaza answers:

what code is being used for the first menu generation, so that it can modified for your needs?

2012-01-10

rizaljohn answers:

You can do that using jQuery. Something like this:

jQuery(".sub-menu menu-item").prepend("<h2> Radio</h2>");

2012-01-10

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())
);

2012-01-13

Julio Potier answers:

Can we see an URL for this menu ? Thank you

2012-01-13

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')) : '';
}