您可以尝试以下操作:
<?php global $my_profile; ?>
<?php if (is_user_logged_in()){
$avatar = \'<div class="img" data-key="profile">\'.get_avatar( get_current_user_id(), 64 ).\'</div>\';
}else{
$avatar = \'\';
} ?>
你的菜单代码应该是这样的
$defaults = array(
\'theme_location\' => \'location of menu in your theme\',
\'menu\' => \'slug\',
\'container\' => \'div\',
\'menu_class\' => \'menu\',
\'echo\' => true,
\'fallback_cb\' => \'wp_page_menu\',
\'items_wrap\' => $avatar.\'<ul id="%1$s" class="%2$s">%3$s</ul>\',
);
wp_nav_menu( $defaults );
根据您的要求填写所有参数。有关参数的完整列表,请查看此页
https://codex.wordpress.org/Function_Reference/wp_nav_menu