Bundled Themes: Twenty Nineteen: Omit irrelevant ARIA in menus.

Don't set `aria-expanded` and `aria-haspopup` attributes outside of the primary menu. Secondary menus (social and footer) are set to a depth of 1, and have nothing to expand or popup.

Props bschneidewind, joedolson, mikinc860.
Fixes #62896.

git-svn-id: https://develop.svn.wordpress.org/trunk@59921 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Joe Dolson 2025-03-03 22:51:24 +00:00
parent 24a47a419a
commit 1578f25674

View File

@ -167,7 +167,7 @@ add_filter( 'wp_nav_menu', 'twentynineteen_add_ellipses_to_nav', 10, 2 );
*
* @link https://www.w3.org/WAI/tutorials/menus/flyout/
*
* @param array $atts {
* @param array $atts {
* The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
*
* @type string $title Title attribute.
@ -176,21 +176,25 @@ add_filter( 'wp_nav_menu', 'twentynineteen_add_ellipses_to_nav', 10, 2 );
* @type string $href The href attribute.
* @type string $aria-current The aria-current attribute.
* }
* @param WP_Post $item The current menu item object.
* @param WP_Post $item The current menu item object.
* @param stdClass $args An object of `wp_nav_menu()` arguments.
* @return string[] Modified attributes.
*/
function twentynineteen_nav_menu_link_attributes( $atts, $item ) {
function twentynineteen_nav_menu_link_attributes( $atts, $item, $args ) {
// Add [aria-haspopup] and [aria-expanded] to menu items that have children.
$item_has_children = in_array( 'menu-item-has-children', $item->classes, true );
if ( $item_has_children ) {
$atts['aria-haspopup'] = 'true';
$atts['aria-expanded'] = 'false';
// Check that this is the primary menu.
if ( isset( $args->theme_location ) && 'menu-1' === $args->theme_location ) {
// Add [aria-haspopup] and [aria-expanded] to menu items that have children.
$item_has_children = in_array( 'menu-item-has-children', $item->classes, true );
if ( $item_has_children ) {
$atts['aria-haspopup'] = 'true';
$atts['aria-expanded'] = 'false';
}
}
return $atts;
}
add_filter( 'nav_menu_link_attributes', 'twentynineteen_nav_menu_link_attributes', 10, 2 );
add_filter( 'nav_menu_link_attributes', 'twentynineteen_nav_menu_link_attributes', 10, 3 );
/**
* Creates a nav menu item to be displayed on mobile to navigate from submenu back to the parent.