I’m using fullpage.js on a wordpress site and would like to use one of it’s function wicht is to add a class on a li. But to do this you need to add the data attribute data-menuanchor .
I have seen this:
add_filter( 'nav_menu_link_attributes', 'my_nav_menu_attribs', 10, 3 );
function my_nav_menu_attribs( $atts, $item, $args )
{
// The ID of the target menu item
$menu_target = 113;
// inspect $item
if ($item->ID == $menu_target) {
$atts['data-menuanchor'] = 's1';
}
return $atts;
}
It works excepts that it adds the attribute to the a tag and need it to be on the li tag.
Also, if I want to do it to all the items on the menu. Do I need to do this function for all the items? And I suppose I have to rename the function each time?
Thanks
I’m not sure WP has a specific filter/method for this. You’re gonna have to do some manual DOM manipulation along with
wp_nav_menu_items
.https://developer.wordpress.org/reference/hooks/wp_nav_menu_items/
I solved this, using the custom links and getting the url entered in the link as an attribute and replacing the # character with empty. The following code must be entered in the file functions.php:
Attached screenshot of my menu.
Any questions tell me and I gladly help them.
Luck.
Actually i was searching for a similar thing for fullpage.js navigation,i managed it setting up a filter that get the pre-existing title-attr customizable from backend and assign it to ‘data-menuanchor’ just to keep it at least a minimum scalable and mantainable even if i have to add something new.
Here’s what i’ve used.