Search code examples
phpwordpresswp-nav-walker

Error when editing WordPress Menu with Walker Class


I am currently following a YouTube tutorial on how to edit the WordPress Menu. The tutorial has suggested to include the following code within the 'walker.php' file:

function start_lvl( &$output, $depth ){
        $indent = str_repeat("\t",$depth);
        $submenu = ($depth > 0) ' sub-menu' : '';
        $output .= "\n$indent<ul class=\"dropdown-menu$submenu depth_$depth\">\n";
    } 

When I do include this code, my website returns an 'HTTP ERROR 500' page. Through commenting out each line of the code, in a method of elimination fashion, I have isolated the problematic code to:

$submenu = ($depth > 0) ' sub-menu' : '';

I have checked out other similarly titled questions here but unable to work out what the issue could be. Is anyone able to see where I am going wrong with this particular code?

I am not sure if the HTML Markup plays a role in the error but just in case, here is the code for said markup:

wp_nav_menu()
    <div class="menu-container">
        <ul> 
            <li><a>Link<span></a></span></li>
            <li><a>Link<span></a></span></li>
            <li><a>Link<span></a></span></li>
             <li><a>Link<span></a></span></li>
            </ul>   
    </div>

Solution

  • $submenu = ($depth > 0) ' sub-menu' : ''; should be:

    $submenu = ($depth > 0)?' sub-menu' : '';

    You are missing ? in the shorthand if else condition.