如何在Wordpress中将Gravatar设置为背景图片?

问题描述:

我正在开发一个自定义WordPress主题,并根据设计要求,我必须在图像的背景中设置作者Gravatar。如何在Wordpress中将Gravatar设置为背景图片?

我已经尝试使用get_avatar_url()wp_get_attachment_image_src如下,但未能实现:

PHP内嵌代码:

<?php $thumb = wp_get_attachment_image_src(get_avatar_url($post->ID), '80'); ?> 

<div class="author" style="background-image: url('<?php echo $thumb[0]; ?>')"></div> 

CSS:

.author { 
width:120px; 
height:120px; 
background: no-repeat center; 
} 

如何做到这一点?

我发现在这里的答案(https://*.com/a/29860844/3725816):

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=120'; 
echo $user_gravatar_url; 
} 


<div class="author" style="background: url('<?php echo my_gravatar_url(); ?>') center center no-repeat; top: 50%; left: 50%;"> 
</div>