0
votes

I want to add a <li class="divider"></li> into the navigation submenu of WordPress. That is in between each of the sub menu item I want add this <li>. Currently I am using Walker_Nav_Menu to show description of the main navigation menu using

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-text">' . $item->description . '</span>';
        $item_output .= '</a>';
        $item_output .= $args->after;

        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }


function start_lvl(&$output, $depth) {
    $indent = str_repeat("\t", $depth);
    $output .= "\n$indent<ul class=\"dropdown-menu\">\n";
  }

}

Also I am changing the sub menu class to "dropdown-menu". I also want to change the class of <a> inside <li class="dropdown"> to <a href="#" class="dropdown-toggle" data-toggle="dropdown">. So the menu structure I want is

<ul class="nav navbar-nav">         
<li><a href="link1.html">link1<br>
<span class="sub-text">text here<span> </a></li>
<li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">link2<br>
<span class="sub-text">text 2<span></a>

<ul class="dropdown-menu">
<li><a href="link3.html">link3</a></li>
<li class="divider"></li>
<li><a href="link4.html">link4</a></li>
<li class="divider"></li>
<li><a href="link5.html">link5</a></li>
</ul>
</li>
</ul>

Please help.

1

1 Answers

0
votes

You should update the following line:

$output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';

To:

if( $depth==0 ){
    if($item->menu_order > 1) $output .= $indent . '<li class="divider"></li>';
    $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
}else{
    $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
}

An to change the class of A inside li class="dropdown", edit the line:

$item_output .= '<a'. $attributes .'>';

To:

if( $depth==0 )
    $item_output .= '<a'. $attributes .' class="dropdown-toggle" data-toggle="dropdown">';
else
    $item_output .= '<a'. $attributes .'>';

You have more controls, but all depends on your needs and of course by your tricks :)