Display avatar of user profile when logged in

I am trying to display user’s avatar/profile image when logged in, but when I use this:

 <?php
    global $current_user;
    get_currentuserinfo();     
    echo get_avatar( $current_user->ID, 64 );

?>

it displays the default_avatar_male.jpg, but I don’t want anything to be displayed unless logged in. Thanks for all help.

Solutions Collecting From Web of "Display avatar of user profile when logged in"

Hey pass the current user email id in get_avatar() function if user is logged in like this

 <?php 
        if ( is_user_logged_in() ):
            $current_user = wp_get_current_user();

        if ( ($current_user instanceof WP_User) ) {
            echo 'Welcome : ' . $current_user->display_name ;
            echo get_avatar( $current_user->user_email, 32 );
        }
        endif;

    ?>

You can wrap this code in to a condition that if user is logged in then show profile pic and for that you can use wordpress function is_user_logged_in.

So your code will be something like this :

<?php
    global $current_user;
    if ( is_user_logged_in() ):
        get_currentuserinfo();     
        echo get_avatar( $current_user->ID, 64 );
    endif;  
?>