I’m using wp_nav_menu() to display menu items with a custom walker.

<ul>
<li id="menu-item-799" class="menu-item ... custom-theme-class"><a href="#">custom content</a></li>
<li><a href=""></a></li>
<li><a href=""></a></li>
...
</ul>

Now I’d like to change the li output depending on a class name. I use something like this:

function theme_function($item_output, $item, $depth, $args){    

    $output = '';       

    // Overview Menu
    if ( in_array( 'custom-theme-class', (array) $item->classes ) ) {
        $item_output = '<div class="navbar-item">'. wp_kses( $item->title, wp_kses_allowed_html( 'post' ) ) .'</div>';
    }   

        return $item_output;  

    }
    add_filter('walker_nav_menu_start_el', 'theme_function', 10, 4);    

This simply changes

<li ...><a href="#">custom content</a></li>

to

<li ..."><div class="navbar-item">custom content</div></li>

While I wish to remove the li tags as well. So it becomes only

<div class="navbar-item">custom content</div> (without the li tags)

In my custom menu walker I used the following but it doesn’t work.

function start_el(&$output, $item, $depth = 0, $args = array() , $id = 0) {

    ... 

    if ( in_array( 'custom-theme-class', (array) $item->classes ) ) {
        $output .= '';
    } else {
        $output .= $indent . '<li' . $id . $value . $class_names . $data_dropdown . '>';
    }

}


function end_el(&$output, $item, $depth = 0, $args = array()) {

    $classes = empty($item->classes) ? array() : (array)$item->classes;

    $has_children = in_array('menu-item-has-children', $classes);

    if ($has_children && $depth == 0) {            
        $output.= "</div>n";
    }

    if ( in_array( 'custom-theme-class', (array) $item->classes ) ) {
        $output .= '';
    } else {
        $output.= "</li>n";
    }

}

The li tags still shows when a menu is used with the custom-theme-class css class.

What am I missing?
Any help is much appreciated.

Read more here: Custom menu walker – remove li tag based on class name


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: