Remove li from wp_nav_menu


Updated on: June 10, 2020 in Wordpress by

Here is my updated attempt based on the resource shared by @shahar:

<?php wp_nav_menu( array('menu' => 'Main', 'walker' => new Footer_Walker()) ); ?>

And in my functions.php:

class Footer_Walker extends Walker_Nav_Menu {
  function start_el(&$output, $item, $depth=0, $args=array()) {
    $output .= "<div>".esc_attr($item->label);
  }

  function end_el(&$output, $item, $depth=0, $args=array()) {
    $output .= "</div>\n";
  }
}

Using the code above the menu is not displaying at all.


Update: Here is the final code I ended up using in my functions file:

class Footer_Walker extends Walker_Nav_Menu {
  function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
    $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 class='col-sm-6 col-md-3'>";
    $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 );
  }
}

And here’s the code to generate the menu in your template:

<?php wp_nav_menu(array('menu' => 'Footer', 'items_wrap'=> '%3$s', 'walker' => new Footer_Walker(), 'container'=>false, 'menu_class' => '', 'theme_location'=>'footer', 'fallback_cb'=>false )); ?>

menuswalker