admin管理员组

文章数量:1287649

I need to get the post author profile picture link.

I know I can use get_avatar(); but it displays whole image markup

<img src="http://.../img.png" class="avatar img-responsive" alt="image">

But need only the src of the author profile pic.

How can I got that ? Maybe we can use some php or js to take out the only src value from the total output.

My second question is, how can I add that image as post_thumbnail if no thumbnail exist for that post ?

I need to get the post author profile picture link.

I know I can use get_avatar(); but it displays whole image markup

<img src="http://.../img.png" class="avatar img-responsive" alt="image">

But need only the src of the author profile pic.

How can I got that ? Maybe we can use some php or js to take out the only src value from the total output.

My second question is, how can I add that image as post_thumbnail if no thumbnail exist for that post ?

Share Improve this question asked Sep 5, 2015 at 20:54 hkchakladarhkchakladar 3321 gold badge3 silver badges13 bronze badges 4
  • Answer is here: wordpress.stackexchange/a/188288/24875 You simply use the get_avatar_url() function. – Christine Cooper Commented Sep 5, 2015 at 21:24
  • Oh, I see it is a new feature, so I didn't got it in codex. Any comment on my second part of the question ? – hkchakladar Commented Sep 5, 2015 at 21:47
  • in The loop you can check if thumbnail exist with function has_post_thumbnail(). In case of false display your image. – Alexey Commented Sep 5, 2015 at 22:35
  • Possible duplicate of How do I get the avatar URL instead of an HTML IMG tag when using get_avatar? – Andy Macaulay-Brook Commented Oct 8, 2016 at 10:52
Add a comment  | 

5 Answers 5

Reset to default 9

Putting the following inside loop should fulfill your needs:

<?php
$get_author_id = get_the_author_meta('ID');
$get_author_gravatar = get_avatar_url($get_author_id, array('size' => 450));

if(has_post_thumbnail()){
    the_post_thumbnail();
} else {
    echo '<img src="'.$get_author_gravatar.'" alt="'.get_the_title().'" />';
}
?>

just use the function get_avatart_uri() and pass in the user's email as the parameter it will give you the avatar image url

Beside using get_avatar_url, you can still use get_avatar. If I'm not wrong, this is what you're looking for.

Question 1

Get the post author profile picture link


<?php 
    // only output if gravatar function exists
    if (function_exists('get_avatar')) { ?>

    <?php
    // Convert email into md5 hash and set image size to 32px
    $gravatar = 'http://www.gravatar/avatar/' . md5(strtolower($user_email)) . '&s=32';

    // Convert email into md5 hash and remove image size to use as post image
    $gravatar_bg = 'http://www.gravatar/avatar/' . md5(strtolower($user_email)) . '';
    ?>
    <!-- Output the gravatar as img src -->
    <img src="<?php echo "$gravatar";?>" alt="">

    <!-- Output the gravatar as background url -->
    <div class="avatar" style="background: url(<?php echo $gravatar_bg ?>);" ></div>

<?php } ?>

Question 2

Add that image as post_thumbnail if no thumbnail exist for that post


<?php
    // Check if post has thumbnail 
    // if ( has_post_thumbnail() ) {            // option 1
    if ( '' != get_the_post_thumbnail() ) {     // option 2

    // if has, output the thumbnail
      the_post_thumbnail();

    } else {
     // if not, show the gravatar image
     echo '<img src="<?php echo "$gravatar";?>" alt="">';
    }
?>

Hope this helps, good luck!


Resources: Using Gravatars | hast_post_thumbnail

UPDATE(22 May, 2021) - I am using a plugin Simple Local Avatars to allow users to update images locally. The below solution will only work if you use this plugin.


Just In case someone is looking for full image of author and not finding it. Here is a little hack.

Simple Method

$avatar_url  = get_avatar_url( $author_id ); //this will give 96x96 image.
$final_url   = str_replace('-96x96.', '.',$author_avatar_url);

Replacing the -96x96. with just a ., Just need to make sure Image name doesn't include this string. Alternatively you can look for the same string from reverse and do the replace using substr_replace() like:

More Accurate Method

$avatar_url  = get_avatar_url( $author_id ); //this will give 96x96 image.
$replace   = '-96x96.';
$pos = strpos($avatar_url, $replace, -(strlen($replace)+5)); // +5 for length of ext
$final_url = substr_replace($avatar_url, '.', $pos, strlen($replace));

This is the first result on google while looking for full image of author so it might help someone.

This is all I do to add the registered user's avatar.

<img src="<?php echo get_avatar_url(get_the_author_meta('ID')); ?>" class="YOUR CSS CLASS">

本文标签: avatarGet only the author profile picture image url inside a loop