How to get author's Gravatar url?

醉酒当歌 提交于 2019-12-08 12:30:47

问题


I want the author's pic to appear in some part of the single.php page, and I used this code I found somewhere on the wordpress stack exchange to get the URL of the author Gravatar in order to use the pic as a background image of a DIV :

This code in functions.php :

if ( function_exists('register_sidebar') )
    register_sidebar();
add_theme_support( 'post-thumbnails' );
add_image_size( 'related', 300 , 200 , array(bottom, right) ); 
function get_avatar_url($get_avatar){
    preg_match("/src='(.*?)'/i", $get_avatar, $matches);
    return $matches[1]; };

And this in the single.php :

<div style="background:url('<?php echo get_avatar_url(get_avatar( $curauth->ID, 150 )); ?>');"> </div>

The strange thing about it is that it returns the default "mystery man" avatar instead of the author's Gravatar, although the Gravatar is linked to the author account and it appears on the admin panel !

How can I solve this problem ?


回答1:


This function will simply give you GRAVATAR URL!

Thanks Musik8101, I came up with my custom solution based on your code.

Here it is and it's working good:

 function my_gravatar_url() { // Get user email
$user_email = get_the_author_meta( 'user_email' );
// Convert email into md5 hash and set image size to 80 px
$user_gravatar_url = 'http://www.gravatar.com/avatar/' . md5($user_email) . '?s=80';
echo $user_gravatar_url; } 

in Template just use:

<?php my_gravatar_url() ?>

Notice: it must be used inside a loop. Works on Wordpress 4.2 for those who had problems with get_avatar_url method.




回答2:


Wordpress comes built in with the get_avatar function. What you are doing is a little bit of both getting the avatar through a custom function and using wordpress to get it with the get_avatar function. You can read more about it here

You can call it right from the single.php page and size it right from the same code without your function.

<?php echo get_avatar( $id_or_email, $size, $default, $alt ); ?> 

Here are some examples

For comments:

<?php echo get_avatar( $comment, 32 ); ?>

For a post's author, in The Loop:

<?php echo get_avatar( get_the_author_meta( 'ID' ), 32 ); ?>

For a random email address:

<?php echo get_avatar( 'email@example.com', 32 ); ?>

Update from comment:

Either of these two ways will solve your problem. Why would you try to retrieve the url of a gravatar so that you can use that url to display the gravatar when you can just display the gravatar? Gravatar does this dynamically with security and the way you do it is by getting the authors email and calling to gravatar to return the image from that email. I don't see the point of making it a background to a div in css when you can just position your divs. You can call the Gravatar in one div and position another div that contains text over it or anything you could do by using it as a div background.

The way Gravatar works is by Dynamically retrieving a user's image from a server. If everyone's gravatar was displayed by referencing the img url it would be coded very differently. Wordpress has an article called Using Gravatars that has a good explanation of it.

If it makes more sense to you this way you you could place in a plugin or your functions.php but it's ultimately the same thing. You want to call that Gravatar and secure it, then simply place it in a div and style it. That's the only way I know how to explain it.

 function my_display_gravatar() { 
    global $author;
    get_the_author_meta();
    // Get User Email Address
    $getuseremail = $author->user_email;
    // Convert email into md5 hash and set image size to 200 px
    $usergravatar = 'http://www.gravatar.com/avatar/' . md5($getuseremail) . '?s=200';
    echo '<img src="' . $usergravatar . '" class="wpb_gravatar" />';
} 

Then call the function with

<div id="gravatar">
<?php echo my_display_gravatar(); ?>
</div>

If you are serving over an ssl remember to use https://secure.gravatar.com

Let me know if you get what I'm saying.



来源:https://stackoverflow.com/questions/23330459/how-to-get-authors-gravatar-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!