在开发wordpress的时候碰到了一个棘手的问题,wp内置函数是无法直接获取头像的url的,这就很蛋疼了,虽然你想其他的办法也能够实现获取,但是作为三好青年,肯定不能就这样算了的,于是就研究了一下。
Gravatar官方开发文档上已经告诉我们,简单地获取一个头像的链接是这样的链接:
https://www.gravatar.com/avatar/电子邮箱的hash值?s=头像尺寸
那这样就好办了,我们按照文字提示填写就行了,如果你是php,获取电子邮箱的hash值就很简单了,使用md5()
函数就能获取。
至于电子邮箱,就看自己的了,最简便的方法是自己填写,那么如果你使用wp的话,就可以做的更加灵活:
<?php get_the_author_meta('user_email', $post->post_author); ?>
使用上面的代码就可以获取用户字段中的邮箱,最后将上面的代码组合一下:
<?php
$on_getting_the_link_of_gravatar_s_head_image = 'https://www.gravatar.com/avatar/' . md5(get_the_author_meta( 'user_email', $post->post_author )) . '?s=200';
echo $on_getting_the_link_of_gravatar_s_head_image;
?>
当然了,Gravatar头像链接一直对国内用户不辣么地友好,所以又优化了一下代码(感谢loli.net推出的这个服务):
<?php
$on_getting_the_link_of_gravatar_s_head_image = 'https://gravatar.loli.net/avatar/' . md5(get_the_author_meta( 'user_email', $post->post_author )) . '?s=200';
echo $on_getting_the_link_of_gravatar_s_head_image;
?>
2333
2021-01-16 15:19
渔夫
2021-01-16 22:29
可以有,但是难推广
日记哥
2020-09-09 17:34
刚好在找头像相关的帖子
一叶三秋🐾
2020-08-03 11:29
不明觉厉,先插个眼,感觉以后用得到
1梦
2020-08-01 17:57
牛逼