Custom menu walker: how can i check first item

Count the level 0 elements in a static variable in the method and add an extra class if you hit the third. Sample code:

function start_lvl(&$output, $depth) {
    static $column = 1;
    $indent = str_repeat("\t", $depth);
    if ($depth > 0)
    {
        $output .= "\n$indent<ul class="subsubmenu">\n";
    }
    else
    {
        $column += 1;
        $extra = 3 === $column ? ' third-column' : '';
        $output .= "\n$indent<ul class="submenu$extra">\n";
    }
}

Leave a Comment