2016-02-29 170 views
1

我正在開發一個自定義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; 
} 

如何做到這一點?

回答

1

我發現在這裏的答案(https://stackoverflow.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>