2017-08-06 31 views
0

你能幫我解決我的問題嗎?如何在WordPress中獲取get_avatar_url函數的URL

我想要做的是獲取WordPress中get_avatar_url()函數的確切URL。然後使用該URL作爲背景圖像網址。

下面是我的代碼的樣子。

<?php $avatar_url = get_avatar_url(get_avatar($author->ID, 150), array("size"=>260)); ?> 
<div class="col-xs-5 nopadding author-picture" style="background: url('<?php echo $avatar_url; ?>')"> 
    <a class="author-block " href="<?php echo get_author_posts_url($author->ID); ?>"> 
      <figure class="author-picture"> 
       <?php echo get_avatar($author->ID, 150); ?> 
      </figure> 
    </a> 
</div> 

然而,在頁面上它返回這樣的: enter image description here

回答

0

get_avatar_url用法get_avatar_url(mixed $id_or_email, array $args = null)。爲什麼你通過get_avatar這個函數。

您可以像這樣直接使用此:

get_avatar_url($author->ID, array("size"=>260)); 
+0

嗨阿米特,感謝您的快速反應。在我發佈問題之前,我一直在谷歌上搜尋可能的解決方案。您看到的代碼是我在網上看到的建議中嘗試的最後一個代碼。 我試過你的答案,但它仍然沒有返回圖像的實際URL。 或者,也許我在我的問題中使用了錯誤的術語。我的目標是獲取get_avatar函數的「src」屬性的值,以便我可以在background-image url上使用它。 – Franz

+0

但是'get_avatar_url'應該返回'get_avatar'用於創建它所創建的'img'元素中'src'的url。所以使用該功能就足夠了。 –

+0

嗨阿米特,不幸的是,這是我得到的回報。 background:url(https://secure.gravatar.com/avatar/e57e294e57b764ab4f6ffc3ee2d07466?s=260&d=mm&r=g – Franz

0

這樣的:

<?php $avatar_url = get_avatar_url(get_the_author_meta('ID'), array('size' => 450)); ?> 

<div class="col-xs-5 nopadding author-picture" style="background: url('<?php echo esc_url($avatar_url); ?>')"> 
+0

嗨Mo'men,不幸的是它仍然返回相同的東西。 這就是它返回的結果: https://secure.gravatar.com/avatar/d8ce87606c540f1d8bf07bcff50e5516?s=450&d=mm&r=g – Franz

+0

把這個函數放在你的functions.php中| function get_avatar_url($ get_avatar){ preg_match(「/ src ='(。*?)'/ i」,$ get_avatar,$ matches); return $ matches [1]; } –

+0

嗨Mo'men,我按照你的建議在functions.php文件中添加了腳本,但不幸的是頁面變空了。因爲它根本沒有加載整個HTML。 – Franz

相關問題