nav_menu_link_attributes过滤菜单项a元素的class属性

apply_filters( 'nav_menu_link_attributes', array $atts, WP_Post $item, stdClass $args, int $depth )
参数说明类型可选值默认值
$atts应用于菜单项的<a>元素的HTML属性,空字符串将被忽略array
$item当前菜单项
$argswp_nav_menu()参数的对象
$depth菜单项的深度。用于填充int
function nav_menu_link_attributes_fun ($atts, $item, $args) {
  $menu_item_parent = $item->menu_item_parent;
  if ($menu_item_parent == 0) {
    $atts['class'] = 'nav-link';
  }
  return $atts;
}
// 过滤菜单项a元素的class属性
add_filter('nav_menu_link_attributes', 'nav_menu_link_attributes_fun', 10, 3);
评论
:broken_heart: :confounded: :flushed: :frowning: :grinning: :heart: :kissing_heart: :mask: :pensive: :rage: :relaxed: :scream: :smile: :smirk: :sob: :stuck_out_tongue_closed_eyes: :stuck_out_tongue_winking_eye: :wink: