[wp-trac] [WordPress Trac] #52558: Menu items need more classes
WordPress Trac
noreply at wordpress.org
Wed Jun 25 14:55:35 UTC 2025
#52558: Menu items need more classes
----------------------------+------------------------------
Reporter: ericgreenfield | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Menus | Version:
Severity: normal | Resolution:
Keywords: dev-feedback | Focuses: template
----------------------------+------------------------------
Comment (by callumbw95):
Hi @ericgreenfield,
I agree that the classes on menu elements could do with an additional
level of detail for top level menu items, etc. You can include this via a
[https://developer.wordpress.org/reference/classes/walker_nav_menu/| Menu
Walker], which is very similar to the filter you added.
For example we can include a menu walker by including the php class within
the function that calls the menu:
{{{#!php
<?php
wp_nav_menu([
'theme_location' => 'your_menu_location',
'walker' => new Top_Level_Menu_Walker(),
]);
}}}
Example Menu Walker class:
{{{#!php
<?php
class Top_Level_Menu_Walker extends Walker_Nav_Menu {
public function start_el( &$output, $item, $depth = 0, $args = null,
$id = 0 ) {
if ( 0 === $depth ) {
$item->classes[] = 'top-level-item';
}
parent::start_el( $output, $item, $depth, $args, $id );
}
}
}}}
But it would be good as you said to have this in core too so we don't need
to extend this functionality every time we want to have this class
available. Perhaps to make this scalable we should include something that
puts in the depth variable into a class? For example something like:
{{{#!php
<?php
$item->classes[] = 'menu-item-depth-' . $depth; // "menu-item-depth-0",
"menu-item-depth-1", etc.
}}}
If we can get a bit of a discussion happening on this ticket we can see
what the community and core contributors think about this! 😀
--
Ticket URL: <https://core.trac.wordpress.org/ticket/52558#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list