users - Custom Field Repeating When Using foreach

I'm trying to display user avatars by login user name using custom fields like this -And here's the code -func

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 )

        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.

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

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 )

        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.

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

Share Improve this question edited Aug 29, 2019 at 8:29 Brad Dalton asked Aug 29, 2019 at 8:16 Brad DaltonBrad Dalton 6,9852 gold badges36 silver badges47 bronze badges 1
  • 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 Commented Aug 29, 2019 at 19:46
Add a comment  | 

1 Answer 1

Reset to default 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;
              }
           }
        }
    }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745200042a4616280.html

相关推荐

  • users - Custom Field Repeating When Using foreach

    I'm trying to display user avatars by login user name using custom fields like this -And here's the code -func

    5小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信