Created
November 18, 2019 18:04
-
-
Save RiodeJaneiroo/8d6012bbb3387e3d8ba3f439b65247f7 to your computer and use it in GitHub Desktop.
[WordPress – change class in menu]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Изменяем атрибут class у тега li | |
add_filter( 'nav_menu_css_class', 'filter_nav_menu_css_classes', 10, 4 ); | |
function filter_nav_menu_css_classes( $classes, $item, $args, $depth ) { | |
if ( $args->theme_location === 'header_menu' ) { | |
if($depth == 1) { | |
$classes = ['header-podmenu_list']; | |
} else { | |
$classes = ['header-menu_list']; | |
} | |
if ( $item->current ) { | |
$classes[] = 'active'; | |
} | |
} | |
return $classes; | |
} | |
// Изменяет класс у вложенного ul | |
add_filter( 'nav_menu_submenu_css_class', 'filter_nav_menu_submenu_css_class', 10, 3 ); | |
function filter_nav_menu_submenu_css_class( $classes, $args, $depth ) { | |
if ( $args->theme_location === 'header_menu' ) { | |
$classes = ['header-podmenu']; | |
} | |
return $classes; | |
} | |
// Добавляем классы ссылкам | |
add_filter( 'nav_menu_link_attributes', 'filter_nav_menu_link_attributes', 10, 4 ); | |
function filter_nav_menu_link_attributes( $atts, $item, $args, $depth ) { | |
if ( $args->theme_location === 'header_menu' ) { | |
if($depth == 1) { | |
$atts['class'] = 'header-podmenu_link'; | |
} else { | |
$atts['class'] = 'header-menu_link'; | |
} | |
if ( $item->current ) { | |
$atts['class'] .= ' active'; | |
} | |
} | |
return $atts; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment