Skip to content

Instantly share code, notes, and snippets.

@spigotdesign
Created December 13, 2021 16:39
Show Gist options
  • Save spigotdesign/5d13c526fe1e4308458123fee53da2c3 to your computer and use it in GitHub Desktop.
Save spigotdesign/5d13c526fe1e4308458123fee53da2c3 to your computer and use it in GitHub Desktop.
Accessible WP menu
class Spigot_Walker extends Walker_Nav_Menu {
function start_el(&$output, $item, $depth=0, $args=array(), $id = 0) {
$object = $item->object;
$type = $item->type;
$title = $item->title;
$permalink = $item->url;
$classes = $item->classes;
$output .= "<li id='menu-item-". $item->ID ."' class='menu__item " . implode(" ", $classes) . "'>";
if( $permalink && $permalink != '#' ) {
$output .= '<a class="menu__link" href="' . $permalink . '">';
} else {
$output .= '';
}
if($args->walker->has_children) {
$output .= '<button type="button" class="dropdown__title" aria-expanded="false" aria-controls="nav-dropdown-'. $item->ID .'">' ;
}
$output .= $item->title;
if($args->walker->has_children) {
$output .= '</button>' ;
}
if( $permalink && $permalink != '#' ) {
$output .= '</a>';
} else {
$output .= '';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment