WordPress добавить класс в меню навигации
// add a custom class to nav_menu li
function add_additional_class_on_li($classes, $item, $args) {
if (isset($args->add_li_class)) {
$classes[] = $args->add_li_class;
}
return $classes;
}
add_filter('nav_menu_css_class', 'add_additional_class_on_li', 1, 3);
// add a custom class to nav_menu anchor
function add_additional_class_on_anchor($classes, $item, $args) {
if (isset($args->add_anchor_class)) {
$classes['class'] = $args->add_anchor_class;
}
return $classes;
}
add_filter('nav_menu_link_attributes', 'add_additional_class_on_anchor', 1, 3);
// code on header
@php
$nav_classes = [
'theme_location' => 'primary_navigation',
// div tag
'container_class' => 'ml-auto',
// ul tag
'menu_class' => 'nav justify-content-center',
//li tag
'add_li_class' => 'nav-item',
//a tag
'add_anchor_class' => 'nav-link',
];
if (has_nav_menu('primary_navigation')) {
//display navigation menu
echo wp_nav_menu($nav_classes);
}
@endphp
Ivan Cuaco