WordPress.org

Make WordPress Core

Opened 8 months ago

Last modified 7 months ago

#40872 new feature request

Add $item argument to start_lvl (nav menu walker)

Reported by: mireillesan Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.7.5
Component: Menus Keywords: reporter-feedback
Focuses: javascript, template Cc:

Description

Would it be possible to add the $item argument inside the start_lvl function?

Other than the usual dropdown menu, I'd also like to build megamenus and to make that possible, I need to use different ul tags (along with some JavaScript components). These custom ul tags will be different based on the parent li ((array) $item->classes) and $depth.

Change History (2)

#1 @welcher
7 months ago

  • Keywords reporter-feedback added

@mireillesan thanks for the feature request! Can you add some screenshots or pseudo code? It would go a long way in helping to get your idea across.

#2 @mireillesan
7 months ago

Normally, a custom walker can be like this:

public function start_lvl( &$output, $depth = 0, $args = array() ) {		
				
        if ($this->has_children && $depth >= 1) {		           
			$output .= "{$indent}<div class='uk-navbar-dropdown'>{$n}";
            $output .= "{$n}{$indent}<ul class='uk-nav uk-navbar-dropdown-nav'>{$n}";
        } 
		...
		
    }	


If we can also use the $item argument here, I could easily change the output.

if ( $this->has_children && $depth >= 1 && in_array( 'uk-columns', (array) $item->classes ) ) {
		$output .= "{$indent}<div class='uk-navbar-dropdown-grid'>{$n}";
        ...
	}	


I kept the code simply for easy understanding;
the original code is a bit more complex and not just about a different css class name (as the above example).

Right now, I was able to rely on jquery to unwrap/wrap code as a workaround but I prefer to do this inside the custom walker instead.

Note: See TracTickets for help on using tickets.