get_avatar()是wordpress内置用来获取指定用户id或者邮箱头像的函数,通过get_avatar()函数可以轻易地获取文章作者头像、评论用户头像、指定用户头像,在获取用户头像的同时可以指定图像的大小、默认头像以及头像图片的alt属性值。
函数位于:wp-includes/pluggable.php
1 | get_avatar( mixed $id_or_email, int $size = 96, string $default = '', string $alt = '', array $args = null ) |
$id_or_email – (整数 | 字符串 | 对象)(必须),授受用户ID、邮箱或用户的对像
$size – (整数)(可选)指定头像的尺寸大小,单位为px(像素),默认为96(即96px),最大512
$default – (字符串)(可选)如果用户没有设置gravatar头像,则返回false,即没有头像返回,默认为 “神秘人”,可以指定图片 URL 作为默认头像
$alt – (字符串)(可选)头像 img 标签的 alt 属性内容,默认False
$args – (数组)(可选)设置头像额外的参数,默认为空,可设置以下的参数:
1 | <?php echo get_avatar(get_the_author_meta('email'), 50); ?> |
1 | <?php echo get_avatar(1, 50); ?> |
1 | <?php echo get_avatar('email@example.com', 50); ?> |
1 2 3 4 | <?php $getavatar = get_avatar(get_the_author_meta('email'),50,'','博客吧教程',array('width'=>56,'height'=>56,'rating'=>'X','class'=>array('boke8','leonhere'),'extra_attr'=>'title="博客吧教程"','scheme'=>'http') ); echo $getavatar; ?> |
生成的HTML如下:
1 | <img alt="博客吧教程" src="http://1.gravatar.com/avatar/a172d0cf16101716c82a8c8e46739a68?s=50&d=mm&r=x" srcset="http://1.gravatar.com/avatar/a172d0cf16101716c82a8c8e46739a68?s=100&d=mm&r=x 2x" class="avatar avatar-50 photo boke leon" title="博客吧教程" height="56" width="56"> |