0

I'm trying to display user avatars by login user name using custom fields like this -

enter image description here

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 )

        authors();
}


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.

enter image description here

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

  • I might suggest it can be problematic to use a custom field for this. This permits typographic errors, and intentionally incorrect data. Either of which can result in errors. Is there a reason you need a list of user names, whether they have modified the current document or not? Or, would it be better to calculate a list of names for anybody who has created/modified the current post/page? – Mike Baxter Aug 29 at 19:46
1

if you want to use multiple users, if you will add usernames comma separated in custom field. try below code.

function user_avatar() {
    $user = "";
    $names  = get_post_meta( get_the_ID(), 'user_name', true );
    $user_names = array_map('trim', explode(',', $names));
    if(!empty($user_names))
    {
       foreach ($user_names as $key => $user_name) 
       {
          $user = get_user_by( 'login', $user_name  );
          if($user)
          {
              $gravatar = get_avatar( $user->user_email , 32 );
              echo $gravatar;
          }
       }
    }
}

Edit : I also tried -

$names  = get_post_custom_values( 'user_name' );

I tested this when using the same custom field key user_name with different login usernames for each user.

if you want to use get_post_custom_value(); try below

function user_avatar() {
        $user = "";
        $names  = get_post_custom_values( 'user_name', get_the_ID() );
        if(!empty($names))
        {
           foreach ($names as $key => $user_name) 
           {
              $user = get_user_by( 'login', $user_name  );
              if($user)
              {
                  $gravatar = get_avatar( $user->user_email , 32 );
                  echo $gravatar;
              }
           }
        }
    }
  • Thanks. I use foreach because i might need to display avatars for multiple users. Do i not need to use it? – Michelle Aug 29 at 9:52
  • 1
    so will you add multiple users in custom fields separated by comma ? like user1,user2, user3 – Chetan Vaghela Aug 29 at 9:54
  • Tried that but it didn't work. I think i need to use foreach. – Michelle Aug 29 at 10:00
  • 1
    for multiple users, i have updated my answer. please confirm how you will add multiple user in custom filed? – Chetan Vaghela Aug 29 at 10:05
  • 1
    Let us continue this discussion in chat. – Chetan Vaghela Aug 30 at 4:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.