WordPress.org

Ready to get started?Download WordPress

Forums

wp_nav_menu() how to make 2 levels lists with different elements? (1 post)

  1. Evengar
    Member
    Posted 2 years ago #

    I want to make these menu, by using wp_nav_menu() and custom walker which I found in the internet. Please, help my to modify walker and wp_nav_menu() to get this ul structure:

    <ul class="nav">
                  <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">111<b class="caret"></b></a>
                    <ul class="dropdown-menu">
                      <li><a href="">333</a></li>
                    </ul>
                  </li>
                  <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">222<b class="caret"></b></a>
                    <ul class="dropdown-menu">
                      <li><a href="">222.1</a></li>
                      <li><a href="">222.2</a></li>
                    </ul>
                </ul>
    <div class="tabbable tabs-left">
                <?php  wp_nav_menu( array('menu_class' => 'nav',
                                        'container' => false,
                                        'container_class' => 'main-menu-container',
                                        'theme_location' => 'header-menu1',
                                        'walker'=> new header_Walker_Nav_Menu()
                                        ) ); ?>
              </div>
    class header_Walker_Nav_Menu extends Walker_Nav_Menu {
    	/**
    	 * @see Walker::start_el()
    	 * @since 3.0.0
    	 *
    	 * @param string $output Passed by reference. Used to append additional content.
    	 * @param object $item Menu item data object.
    	 * @param int $depth Depth of menu item. Used for padding.
    	 * @param int $current_page Menu item ID.
    	 * @param object $args
    	 */
    	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;
    		$classes[] = 'menu-item-' . $item->ID;
    
    		$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
    		$class_names = ' class="' . esc_attr( $class_names ) . '"';
    
    		$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
    		$id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
    
    		$output .= $indent . '<li class="dropdown" ' . $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        ) .'"' : '';
    
                    // new addition for active class on the a tag
                    if(in_array('current-menu-item', $classes)) {
                        $attributes .= ' class="active"';
                    }
    
    		$item_output = $args->before;
    		$item_output .= '<a class="dropdown-toggle" data-toggle="dropdown"'. $attributes .'>';
    		$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 );
    	}
    }

    - how to change

      for sub-menu elements?
      - how to make different <li class="drop-down"> and
    • for different menu levels?
      - How to delee id'd and excess classes? f.e: <ul id="menu-header-menu">, <li id="menu-item-122" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-122">

Topic Closed

This topic has been closed to new replies.

About this Topic