0

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.

1

Do you have error logging on? If you're running the latest version of WP, this should be throwing an error because start_el and end_el are public functions.

Also, if you're using a custom walker, you should only use the walker and not your additional theme_function.

class wpseWalker extends Walker_Nav_Menu {
    public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $classes[] = 'menu-item-' . $item->ID;
        if(in_array('custom-theme-class', $classes) {
            $output .= '<div class="navbar-item">' . $value;
        } else {
            $output .= $indent . '<li' . $id . $value . $class_names . $data_dropdown . '>';
        }
    }
    public function end_el( &$output, $item, $depth = 0, $args = array() ) {
        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $classes[] = 'menu-item-' . $item->ID;
        if(in_array('custom-theme-class', $classes) {
            $output .= "</div>\n";
        } else {
            $output .= "</li>\n";
        }
    }
}
  • Debug is on but I don't see any errors. Plus most tutorials about the nav_walker don't mention that I should use public functions, so I'm extra grateful for pointing that out to me! – kiarashi May 22 '17 at 20:50
  • I found that out the hard way recently. Most of the tutorials were written before they put "public" in place, probably when they were making updates for PHP7. It's only in recent versions that you have to add it. :) – WebElaine May 23 '17 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.