Let’s discuss about WordPress advanced developments of navigation. I want to change every ul, li tags into div tags of header menu. The header.wp_nav_menu() is always returns ul , li tags. This is I want to change this.
This is my code.
<ul> <li>Home</li> <li>About</li> <li>Contact</li> </ul>
I want to changes to following format.
<div> <div>Home</div> <div>About</div> <div>Contact</div> </div>
Let’s do this in easy way.
To do this, we need a custom walker. Open your function.php file.
1- Create a custom own class.
class Mymenu_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 ); } }
This class will return the expected output by adding filters.
You can learn more about WordPress wp navigation not working in thesis theme in my other post.
2- Call to the class.
Open your header.php file and call to wp_nav_menu function.
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 Mynav_Walker // custom walker to replace li with div ) );