Adding anchors in WordPress Menus

There’s a specific filter for each nav item’s attributes: nav_menu_link_attributes.

So, you can put in your functions.php file something like:

function mysite_add_anchor( $atts, $item, $args ) {

    $atts['href'] .= ( !empty( $item->xfn ) ? '#' . $item->xfn : '' );

    return $atts;

}

add_filter( 'nav_menu_link_attributes', 'mysite_add_anchor', 10, 3 );

What this does, is check the Link Relationship (XFN), and if it’s not empty, it will add that to the end of your link with the #.

A couple things, if you don’t see “Link Relationship” in the menu item, check the “Screen Options” at the top and check the box. ALSO, you can use almost any of the other properties you aren’t currently using for Advanced Menu Properties.

https://codex.wordpress.org/Appearance_Menus_Screen

Not sure if Link Relationship isn’t the right one, but it’s the one I use the least.

Note: I didn’t test the code.