Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress change header navigation list items to div

Tags:

wordpress

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.

like image 283
Sumith Harshan Avatar asked Aug 28 '12 12:08

Sumith Harshan


People also ask

How do I create a dynamic header menu in WordPress?

To add a custom navigation menu, the first thing you need to do is register your new navigation menu by adding this code to your theme's functions. php file. add_action( 'init' , 'wpb_custom_new_menu' ); You can now go to Appearance » Menus page in your WordPress admin and try to create or edit a new menu.

How do I style a navigation menu in WordPress?

Head over to Appearance » Menus page in your WordPress admin and click on the Screen Options button. Once you have checked that box, you will see that an additional field is added when you go to edit each individual menu item. Now you can use this CSS class in your stylesheet to add your custom CSS.

How do I change the menu appearance in WordPress?

To Edit Appearance, Widgets, and Themes: On the dashboard sidebar at the left-hand side of your screen, you will see a section named Personalize. Underneath this, there are two options, Themes, and Menus. These two options are where you can find all of your appearance customization settings.


1 Answers

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
    )
);
like image 192
The Alpha Avatar answered Oct 12 '22 04:10

The Alpha