• Hi all,

    I’ve written a custom walker class for my navigation menu, to be able to output:

    <ul id="menu-main-menu" class="pull-left"><li><a href="#" class="main-category">onthelogg100</a></li>
    <li><a href="#" class="main-category">MainCategoy</a></li>
    <li><a href="#" class="main-category">MainCategory</a></li>
    <li><a href="#" class="main-category">MainCategory - with children</a>
    <div class="sub-menu"><div class="sub-links pull-left"><a href="#">Sub-item</a>
    <a href="#">Sub-item</a>
    </div></div></li>
    </ul>

    and here’s my code:

    class loggmax_class_walker extends Walker_Nav_Menu{
    
    	// Start level, beginning tags
    	function start_lvl(&$output, $depth=0) {
    	    $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
    	    $output .= "\n$indent";
    	    $output .= '<div class="sub-menu">';
    	    $output .= '<div class="sub-links pull-left">';
    	}
    
    	function end_lvl(&$output, $depth) {
    	    $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
    	    $output .= "</div>";
    	    $output .= '</div>';
    	}
    
    	// Start element (beginning list items)
    	function start_el( &$output, $item, $depth, $args=array(),$current_object_id=0 ) {
    	    $this->curItem = $item;
    	    $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
    	    $class_names = $value = '';
    	    $classes = empty( $item->classes ) ? array() : (array) $item->classes;
    	    $classes[] = 'menu-item-' . $item->ID;
    	    $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
    	    $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
    	    $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
    	    $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
    
    	    $atts = array();
    	    $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
    	    $atts['target'] = ! empty( $item->target )     ? $item->target     : '';
    	    $atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
    	    $atts['href']   = ! empty( $item->url )        ? $item->url        : '';
    	    $atts['slug']   = ! empty( $item->slug )        ? $item->slug      : '';
    	    $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
    	    $attributes = '';
    	    $item_output = '';
    	    foreach ( $atts as $attr => $value ) {
    	        if ( ! empty( $value ) ) {
    	            $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
    	            $attributes .= ' ' . $attr . '="' . $value . '"';
    	        }
    	    }
    
    	    // Check if it is a submenu
    	    if ( $depth == 1 ) {
    	    	$output .= '<a'. $attributes. '>';
    	    	$output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
    	    	$output .= '</a>';
    
    	    } elseif ( $depth == 0 ) {
    		    $item_output .= $args->before;
    		    $item_output .= '<li>';
    		    $item_output .= '<a'. $attributes. ' class="main-category">';
    		    $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
    		    $item_output .= '</a>';
    		    $item_output .= $args->after;
    
    		}
    
    		$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    	}
    }

    .
    So far, the class is working, but an ending list tag </li> appears on all my sub-menu items, like so:

    <ul id="menu-main-menu" class="pull-left"><li><a href="#" class="main-category">MainCategory</a></li>
    <li><a href="#" class="main-category">MainCategoy</a></li>
    <li><a href="#" class="main-category">MainCategory</a></li>
    <li><a href="#" class="main-category">MainCategory - with children</a>
    <div class="sub-menu"><div class="sub-links pull-left"><a href="#">Sub-item</a></li>
    <a href="#">Sub-item</a></li>
    </div></div></li>
    </ul>

    .

    Please how do I get rid of that </li> in the sub-menu items?

    • This topic was modified 8 years, 2 months ago by Sammyskills.
  • The topic ‘Removing the ending list tag in my sub-menu items’ is closed to new replies.