How to remove trailing </li> tags from wp_nav_menu walker

I have used theme twenty thirteen a my base theme and Have tried used code from https://stackoverflow.com/questions/12159486/wordpress-change-header-navigation-list-items-to-div when I try to validate the page I get an error with invalid trailing </li> tags I am trying to create this result from the walker class

<div class="box1">
    <a href="#"><span class="click"></span></a>
</div>
<div class="box2">
    <h1 class="#">text</h1>
</div>
<div class="box3">
    <a href="#"><span class="click"></span></a>
    <h3 class="#">Text<span class="#">></span></h3>
</div>

But always end up with trailing </li> tags can anyone help with this problem

Read More

I tried this first

class Description_Walker extends Walker_Nav_Menu
{
    function start_el(&$output, $item, $depth, $args)
    {
        $classes = empty($item->classes) ? array () : (array) $item->classes;
        $class_names = join(' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
        !empty ( $class_names ) and $class_names = ' class="'. esc_attr( $class_names ) . '"';
        $output .= "<div id='menu-item-$item->ID' $class_names>";
        $attributes  = '';
        !empty( $item->attr_title ) and $attributes .= ' title="'  . esc_attr( $item->attr_title ) .'"';
        !empty( $item->target ) and $attributes .= ' target="' . esc_attr( $item->target     ) .'"';
        !empty( $item->xfn ) and $attributes .= ' rel="'    . esc_attr( $item->xfn        ) .'"';
        !empty( $item->url ) and $attributes .= ' href="'   . esc_attr( $item->url        ) .'"';
        $title = apply_filters( 'the_title', $item->title, $item->ID );
        $item_output = $args->before
        . "<a $attributes>"
        . $args->link_before
        . $title
        . '</a></div>'
        . $args->link_after
        . $args->after;
        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
}

and put this in the header

wp_nav_menu(
    array (
        'menu' => 'main-menu',
        'container' => 'div', // parent container 
        'container_id' => 'my_nav', //parent container ID
        'depth' => 1,
        'items_wrap' => '%3$s', // removes ul
    'walker' => new Description_Walker // custom walker to replace li with div
    )
);

I have tried modifying this code but I all ways get trailing </li> tags do you think you could point me in the right direction been work on this problem for over a week others seem to have the same problem and I have tried what has been suggested but to no avail. by the way this code was provided by RCV

Related posts

2 comments

  1. You need to do something with the function end_el. I made a custom walker to generate a select menu and I didn’t get clean output until I overwrote end_el and generated the closing option tag in there. Trying to do it all in start_el() was causing headaches.

    Try removing the closing div from this line

    . '</a></div>'
    

    and change it to only close your anchor like this.

    . '</a>
    

    then override the end_el function like this

    class Description_Walker extends Walker_Nav_Menu
    {
        function start_el(&$output, $item, $depth, $args)
        {
            $classes = empty($item->classes) ? array () : (array) $item->classes;
            $class_names = join(' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
            !empty ( $class_names ) and $class_names = ' class="'. esc_attr( $class_names ) . '"';
            $output .= "<div id='menu-item-$item->ID' $class_names>";
            $attributes  = '';
            !empty( $item->attr_title ) and $attributes .= ' title="'  . esc_attr( $item->attr_title ) .'"';
            !empty( $item->target ) and $attributes .= ' target="' . esc_attr( $item->target     ) .'"';
            !empty( $item->xfn ) and $attributes .= ' rel="'    . esc_attr( $item->xfn        ) .'"';
            !empty( $item->url ) and $attributes .= ' href="'   . esc_attr( $item->url        ) .'"';
            $title = apply_filters( 'the_title', $item->title, $item->ID );
            $item_output = $args->before
            . "<a $attributes>"
            . $args->link_before
            . $title
            . '</a>'
            . $args->link_after
            . $args->after;
            $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
        }
        function end_el(&$output, $item, $depth) 
        {
            $output .= "</div>";
        }
    }
    

    I’m pretty sure the problem is that you aren’t overiding end_el() so it’s just doing its thing and inserting the closing li tag.

Comments are closed.