Custom Field Repeating When Using foreach

enter image description here

I’m trying to display user avatars by login user name using custom fields like this –

And here’s the code –

function user_avatar() {

        $name  = get_post_meta( get_the_ID(), 'user_name', true );

        $users = get_user_by( 'login', $name  );

        foreach( (array) $users as $user )


function authors() {

    $gravatar = get_avatar( $user->user_email , 32 );

    echo $gravatar;


However, this code repeats avatars in addition to the only one i want printed.

I need to print only the avatars where’s there’s a custom field value based on username.

Read more here:: Custom Field Repeating When Using foreach

Leave a Reply

Your email address will not be published. Required fields are marked *