Archived topic
Filter for #generate-slideout-menu ?
5 replies · Started by akal on October 29, 2022
Hi, I'm looking into a way to add custom attribute to the offcanvas menu (location : slideout).
Right now, I'm using nav_menu_link_attributes filter and I'm able to add my attributes to the ul.slideout-menu > li > a elements.
But I would prefer target the ul.slideout-menu tag (or parents like .main-nav or even the nav#generate-slideout-menu) using filter ?
The only available filter I see for that is generate_menu_class but it's only for the #site-navigation ...
https://docs.generatepress.com/article/generate_navigation_class/
Hi there,
there is the nav_menu_css_class filter which appends the menu list element.
https://developer.wordpress.org/reference/hooks/nav_menu_css_class/
So you could try this:
function db_slideout_menu_list_classes( $classes, $item, $args ) {
if ( 'slideout' === $args->theme_location ) {
$classes[] = "my-custom-class";
}
return $classes;
}
add_filter( 'nav_menu_css_class' , 'db_slideout_menu_list_classes' , 10, 4 );
Hi David, ok I see, I can add custom class to the LI. Is it possible to target the UL ? Thanks
Aah, try this one:
function db_modify_nav_menu_args( $args ) {
if( 'slideout' == $args['theme_location'] ) {
$args['menu_class'] = 'my-custom-class';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'db_modify_nav_menu_args' );
David, many thanks for your help and patience ! I got it ^^
https://developer.wordpress.org/reference/functions/wp_nav_menu/#source
function db_modify_nav_menu_args( $args ) {
if( 'slideout' == $args['theme_location'] ) {
//$args['menu_class'] = 'my-custom-class';
$args['items_wrap'] = '<ul id="%1$s" uk-scrollspy="cls:uk-animation-slide-top; delay:100; repeat: true" class="%2$s">%3$s</ul>';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'db_modify_nav_menu_args' );
Awesome, glad to be of help!