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 ?


Putting the following inside loop should fulfill your needs:

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

} else {
    echo '<img src="'.$get_author_gravatar.'" alt="'.get_the_title().'" />';
| improve this answer | |

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

| improve this answer | |

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

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

    // Convert email into md5 hash and set image size to 32px
    $gravatar = 'http://www.gravatar.com/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.com/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

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

    // if has, output the 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

| improve this answer | |
  • Hi, I know I can check if(get_the_post_thumbnail()) and output that image in template. But that doesn't set the post_thumbnail, it only output on that particular template. I want it to set_post_thumbnail. How to do this ? – hkchakladar Sep 6 '15 at 9:36
  • Based on your comment, I don't quite understand what you're trying to accomplish here. Can you be more specific? You asked how to add that image as post_thumbnail if no thumbnail exist for that post, the code I posted does just that. – Jentan Bernardus Sep 6 '15 at 17:47

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.