I have the following, which successfully inserts a dynamic link as the last top-level menu item of the specified menu:
add_filter( 'wp_nav_menu_items', 'example_last_nav_item', 10, 2 );
function example_last_nav_item( $items, $args ) {
if ( $args - > theme_location == 'account-menu' && is_user_logged_in() ) {
$current_user = wp_get_current_user();
$items .= '<li class="menu-item profile-link"><a href="https://example.com/profile/' . strtolower( str_replace( ' ', '-', $current_user - > user_login ) ) . '/">Profile</a></li>';
}
return $items;
}
But, how would I access this menu's sub-menu and insert the link as the first item of the sub-menu?
Account Menu Top-level
— DYNAMIC LINK HERE
— Sub-menu Item #2
— Sub-menu Item #3