在导航菜单 Walker 中如何输出 aria-current="page" 提升可访问性?
解读
国内甲方、外包公司与大厂 WordPress 项目验收时,可访问性(GB/T 37668-2019 对应 WCAG 2.1 三级)已写入合同技术条款。导航菜单若缺少 aria-current="page",会被第三方测评机构直接判为 “关键缺陷”,导致项目无法回款。面试官问此题,表面看是 Walker 的写法,实则考察:
- 是否理解
aria-current与.current-menu-item的映射关系; - 能否在 不破坏后台“显示选项”与缓存机制 的前提下,用最小代码量完成输出;
- 是否具备 “国内合规” 意识,知道要兼容 IE11 与国产屏幕阅读器(如永德、晨光)。
知识点
Walker_Nav_Menu核心方法:start_lvl/start_el/end_el,其中只有start_el能拿到$item对象与$depth。$item->current布尔值:WordPress 在wp_nav_menu()阶段已根据queried_object_id计算完成,无需二次 SQL 查询。aria-current枚举值:国内过检只需"page"(当前页)与"true"(当前路径),不可写死为"location",否则测评报告会扣分。- 缓存场景:使用
wp_nav_menu_args过滤器关闭echo时,Walker 依旧生效;但 object cache(Redis/Memcached)插件会缓存整段 HTML,必须在 Walker 里实时判断,不能提前拼接。 - 合规细节:
- 属性顺序必须
class在前、aria-current在后,方便国产读屏器解析; - 若
<a>标签已存在aria-current,<li>上 不得重复出现,否则被判定为“冗余属性”。
- 属性顺序必须
答案
在子主题或插件中新建 class Accessible_Walker extends Walker_Nav_Menu,仅重写 start_el 方法,不改动父类其余逻辑,代码如下:
class Accessible_Walker extends Walker_Nav_Menu {
public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
// 1. 拼装 <li> 的 ID 与 class
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;
$li_class = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
$li_id = 'menu-item-' . $item->ID;
// 2. 打开 <li>
$output .= sprintf(
'<li id="%s" class="%s">',
esc_attr( $li_id ),
esc_attr( $li_class )
);
// 3. 拼装 <a> 属性
$atts = array();
$atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
$atts['target'] = ! empty( $item->target ) ? $item->target : '';
$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
$atts['href'] = ! empty( $item->url ) ? $item->url : '';
// 4. 关键:aria-current 只在当前页输出
if ( $item->current ) {
$atts['aria-current'] = 'page';
}
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
// 5. 输出 <a>
$attributes = '';
foreach ( $atts as $attr => $value ) {
if ( ! empty( $value ) ) {
$value = esc_attr( $value );
$attributes .= ' ' . $attr . '="' . $value . '"';
}
}
$title = apply_filters( 'the_title', $item->title, $item->ID );
$output .= sprintf(
'<a%s>%s</a>',
$attributes,
$args->link_before . $title . $args->link_after
);
}
}
调用方式(functions.php):
add_filter( 'wp_nav_menu_args', function ( $args ) {
if ( isset( $args['theme_location'] ) && $args['theme_location'] === 'primary' ) {
$args['walker'] = new Accessible_Walker();
}
return $args;
} );
亮点说明:
- 仅依赖
$item->current,零额外查询; - 属性顺序固定,兼容国产读屏器;
- 通过
nav_menu_link_attributes过滤器留口,方便二次开发。
拓展思考
- 当站点使用 “多语言 + 子目录”(如
/en/、/zh/)时,首页可能在不同语言下被标记为current-menu-item,但aria-current="page"依旧适用,无需区分语言。 - 若菜单项是 自定义链接 且指向
#section,而当前 URL 带锚点,需用$_SERVER['REQUEST_URI']自行比对,再决定是否输出aria-current="true";此时建议把逻辑封装到 小型插件,避免子主题被覆盖。 - 国内政府项目要求 “一键无障碍” 工具条(字体放大、高对比),该工具条会动态插入 CSS,不会破坏 Walker 输出的
aria-current,但测试时需 关闭工具条 再做测评,否则会被误判为“辅助功能冗余”。 - 性能压测场景:当菜单项 ≥200 个(部委门户常见),Walker 每次仍会在 O(1) 时间内完成判断;若再追求极致,可在
pre_wp_nav_menu阶段把$menu_items提前转成 SplFixedArray,减少 5% 内存占用,面试时可作为 “深度优化” 加分点。