6

I want to change every ul,li tags into div tags of wordpress header.wp_nav_menu() is always returns ul ,li.This is I want to change.

<ul>
  <li>Home</li>
  <li>Services</li>
  <li>Products</li>
</ul>

This must be change like this.

<div>
  <div>Home</div>
  <div>Services</div>
  <div>Products</div>
</div> 

Please help me. Thanks.

Sumith Harshan
  • 6,325
  • 2
  • 36
  • 35

2 Answers2

15

You need a custom walker to do that, an modified version of this one answered by toscho, just paste this class in your functions.php

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 );
    }
}

call wp_nav_menu in header.php like following

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
    )
);
Community
  • 1
  • 1
The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • 1
    I'm very appreciating your help.Thanks a lot. – Sumith Harshan Aug 28 '12 at 18:13
  • Hi RVC just tried your code I have one problem though when I inspect the html I have trailing tags any way to remove them – Artful_dodger Nov 07 '13 at 22:39
  • I didn't use any `li` tags in this, how could `li` tags be there, it should output like the example given in the question using `div`. – The Alpha Nov 07 '13 at 22:42
  • I have tried two different themes twenty thirteen and spud and the results leave me with trailing '' tags here is an example from spun theme'' any thoughts as I have been trying to sort this out for a week now I am using the latest wp 3.7.1 I have even reinstalled and I copied and pasted your code into the theme – Artful_dodger Nov 09 '13 at 21:10
  • The class extension is missing the default $id argument which raises a declaration warning in php7. – cptnk Mar 02 '17 at 12:08
-1
<ul class="navbar-nav mx-auto">
        <li class="nav-item dropdown active dropdown-slide">
          <a class="nav-link" href="#"  data-toggle="dropdown">Home
            <span>/</span>
          </a>
          <!-- Dropdown list -->
          <div class="dropdown-menu">
            <a class="dropdown-item" href="index.html">Homepage</a>
            <a class="dropdown-item" href="homepage-two.html">Homepage 2</a>
          </div>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="speakers.html">Speakers
            <span>/</span>
          </a>
        </li>
        <li class="nav-item dropdown dropdown-slide">
          <a class="nav-link" href="#" data-toggle="dropdown">Pages<span>/</span></a>
            <!-- Dropdown list -->
            <div class="dropdown-menu">
              <a class="dropdown-item" href="about-us.html">About Us</a>
              <a class="dropdown-item" href="single-speaker.html">Single Speaker</a>
              <a class="dropdown-item" href="gallery.html">Gallery</a>
              <a class="dropdown-item" href="gallery-two.html">Gallery-02</a>
              <a class="dropdown-item" href="testimonial.html">Testimonial</a>
              <a class="dropdown-item" href="pricing.html">Pricing</a>
              <a class="dropdown-item" href="FAQ.html">FAQ</a>
              <a class="dropdown-item" href="404.html">404</a>
            </div>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="schedule.html">Schedule<span>/</span></a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="sponsors.html">Sponsors<span>/</span></a>
        </li>
        <li class="nav-item dropdown dropdown-slide">
          <a class="nav-link" href="#"  data-toggle="dropdown">News
            <span>/</span>
          </a>
          <!-- Dropdown list -->
          <div class="dropdown-menu">
            <a class="dropdown-item" href="news.html">News without sidebar</a>
            <a class="dropdown-item" href="news-right-sidebar.html">News with right sidebar</a>
            <a class="dropdown-item" href="news-left-sidebar.html">News with left sidebar</a>
            <a class="dropdown-item" href="news-single.html">News Single</a>
          </div>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="contact.html">Contact</a>
        </li>
      </ul>
VN Ken
  • 1