class Desktop_Submenu_Walker extends Walker_Nav_Menu { private $is_mega_menu = false; public function start_el( &$output, $item, $depth = 0, $args = [], $id = 0 ) { // Set mega menu flag only at top level if ( $depth === 0 && in_array( 'mega-header-menu', $item->classes ) ) { $this->is_mega_menu = true; } else if ( $depth === 0 ) { $this->is_mega_menu = false; } parent::start_el( $output, $item, $depth, $args, $id ); } public function start_lvl( &$output, $depth = 0, $args = [] ) { $indent = str_repeat( "\t", $depth ); // Only add wrapper and class if mega-header-menu is active and it's first level if ( $depth === 0 && $this->is_mega_menu ) { $output .= "\n$indent
\n"; } } }