是的,有,钩子是nav_menu_link_attributes
:
apply_filters( \'nav_menu_link_attributes\', array $atts, WP_Post $item, stdClass $args, int $depth )
过滤应用于菜单项的锚元素的HTML属性。
例如,这将添加nav-link
类到<a>
仅当主题位置($args->theme_location
) 正是my-location
:
add_filter( \'nav_menu_link_attributes\', \'my_nav_menu_link_attributes\', 10, 3 );
function my_nav_menu_link_attributes( $atts, $item, $args ) {
if ( \'my-location\' === $args->theme_location ) {
// Get existing classes, if any.
$class = $atts[\'class\'] ?? \'\';
// Now add your custom class(es).
$atts[\'class\'] = "$class nav-link";
}
return $atts;
}