Opened 9 years ago
Last modified 5 months ago
#40872 new feature request
Add $item argument to start_lvl (nav menu walker)
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Awaiting Review | Priority: | normal |
| Severity: | normal | Version: | 4.7.5 |
| Component: | Menus | Keywords: | reporter-feedback close |
| 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 (3)
#2
@
9 years 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.
#3
@
5 months ago
- Keywords close added
Hi @mireillesan,
I have just taken a look into this, and agree that some way of identifying the current item you are on within start_lvl() would be a useful addition to the function. However having used the function myself there is already a work around due to the fact we have access to start_el() as well as the $data_object from within there.
As the Menu_Walker is a class within PHP we can do the following to store our current item, and make this accessible to the start_lvl() function with something like the following:
<?php class ThemeTruck_Nav_Walker extends Walker_Nav_Menu { private $current_item; public function start_lvl( &$output, $depth = 0, $args = null ) { $item = $this->current_item; } public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) { $this->current_item = $item; } }
This is a little bit more hands on, but I believe this is a valid solution to the problem you experienced. Whilst I am in favour of having the $item variable passed to the start_lvl() function directly, because we can get around this issue when creating our own walkers, I am not sure how much traction this will get. Due to this and the fact this ticket has been sitting for 8 years and 2 major releases now, I am going to attach the close tag, however if you have anymore thoughts on this please reopen the ticket. 😃
@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.