In order to increase my theme’s accessibility, I want to add the aria-current="page"
attribute to the menu item that has the class .current_page_item
.
As of right now, I am doing this via jQuery using the following:
var currentitem = $( '.current_page_item > a' );
currentitem.attr( 'aria-current', 'page' );
While this does what I want it to do, I would prefer that PHP handles this particular thing.
I know I can do this either with a custom Nav Walker class or with a filter to nav_menu_link_attributes
. Unfortunately I have not seen any code even remotely near what I want to do.
function my_menu_filter( $atts, $item, $args, $depth ) {
// Select the li.current_page_item element
// Add `aria-current="page"` to child a element
}
add_filet( 'nav_menu_link_attributes', 'my_menu_filter', 10, 3 );
Any help in writing this filter would be appreciated.