Display logged-in User Avatar in Wordpress Nav Menu

2.8k views Asked by At

I'm using a function to display logged-in user name in wordpress navigation menu and I would like to also display logged-in user avatar in the menu but couldn't find a way to do this. So I'm asking for some help :) Here is the function I'm using for the name:

function my_dynamic_menu_items( $menu_items ) {
    foreach ( $menu_items as $menu_item ) {
        if ( '#current-username#' == $menu_item->title ) {
            global $shortcode_tags;
            if ( isset( $shortcode_tags['current-username'] ) ) {
                // Or do_shortcode(), if you must.
                $menu_item->title = call_user_func( $shortcode_tags['current-username'] );
            }    
        }
    }

    return $menu_items;
}
add_filter( 'wp_nav_menu_objects', 'my_dynamic_menu_items' );

I have this code in my functions.php and I call it with #current-username# in a menu item, is there a way to customise this code to also output the avatar? Thanks for any help.

2

There are 2 answers

0
vrajesh On

try this : echo get_avatar( $id_or_email, $size, $default, $alt );

1
Mansukh Khandhar On

you can display user name as like.

add_filter( 'wp_nav_menu_items', 'your_custom_menu_item', 10, 2 );
function your_custom_menu_item ( $items, $args ) {  
    $current_user = wp_get_current_user();
    if(!empty($current_user->user_login))
        $items .= '<li><a href="javascript:void(0)">'.$current_user->user_login.'</a></li>';        
    return $items;
}