2015-09-24 24 views
0

謝謝Rene Korss幫助解決問題幾乎每次都在那裏回答討論。檢查用戶是否有Gravatar [WordPress]

我想檢查發佈評論的用戶是否有頭像?如果用戶有頭像(意味着有gravatar帳戶)顯示gravatar頭像其他顯示圖像,我已經在其他部分定義。任何幫助將被appriciated。

我目前使用此代碼:

if(get_avatar()==1) 
      { 
       echo get_avatar($comment,$size='48',$default='<path_to_url>'); 
      } 
     else 
      { ?> 
       <img src="<?php bloginfo('template_directory'); ?>/img/admin.jpg" alt=""><?php 
      } ?> 

輸出這個代碼只是工作else部分。如果我寫條件爲if(get_avatar()),那麼只有當部分工作。

$註解的值:

stdClass Object ( 
[comment_ID] => 9 
[comment_post_ID] => 104 
[comment_author] => Navnish 
[comment_author_email] => [email protected] 
[comment_author_url] => 
[comment_author_IP] => 118.146.54.35 
[comment_date] => 2015-09-23 14:33:11 
[comment_date_gmt] => 2015-09-23 14:33:11 
[comment_content] => this is comment by Admin 
[comment_karma] => 0 
[comment_approved] => 1 
[comment_agent] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0 
[comment_type] => 
[comment_parent] => 0 
[user_id] => 1 
) 

當使用此代碼:

<?php 
$avatar = get_avatar(get_the_author_meta('ID'), $size = '48', $default = bloginfo('template_directory').'/img/admin.jpg'); 
if($avatar !== false) 
{ 
echo $avatar; 
} 
?> 

了輸出像這樣:

enter image description here

輸出爲:

$avatar = get_avatar($comment->comment_author_email, $size = '48', $default = bloginfo('template_directory').'/img/admin.jpg'); 
if($avatar !== false) 
{ 
    echo $avatar; 
} 

是: enter image description here

+0

向我們展示如何獲得'$ comment'。 –

+0

$ comment是stdClass對象。我更新了有問題的$ comment的值 –

回答

2

與此嘗試。使用作者電子郵件應該有幫另外,請注意,我不會撥打get_avatar兩次。不需要else,因爲如果不存在虛擬形象,您可以設置要使用的圖像$default

$avatar = get_avatar($comment->comment_author_email, $size = '48', $default = bloginfo('template_directory').'/img/admin.jpg'); 
if($avatar !== false) 
{ 
    echo $avatar; 
} 
+0

獲取錯誤。使用此代碼的輸出更新了問題。請看看它。 –

+0

這不是一個錯誤。它看起來像化身的URL?但它不應該。什麼缺少圖像'src'? –

+0

試過img src。但是,只有我定義的圖像正在工作。 Gravatar avtar無法正常工作。你可以說,它返回的結果是我們在開始時使用的代碼。 –

0

get_avatar()檢索誰提供的用戶ID或郵件地址的用戶的頭像。如果頭像存在,它將返回true。

if(get_avatar($comment)) 
{ 
    echo get_avatar($comment,$size='48',$default='<path_to_url>'); 
} 
else 
{ ?> 
<img src="<?php bloginfo('template_directory'); ?>/img/admin.jpg" alt=""><?php 
} 
+0

仍然只有IF部分與if(get_avatar())一樣工作 –

+0

如何獲取$ comment –

+0

使用函數作爲參數。我已更新有問題的$評論的值。 –

0
if(get_avatar('user_id')==1) 
      { 
       echo get_avatar($comment,$size='48',$default='<path_to_url>'); 
      } 
     else 
      { ?> 
     <img src="<?php bloginfo('template_directory');>/img/admin.jpg" alt=""><?php 
      } ?> 

使用get_avatar('id')代替get_avatar()使用一些ID

+0

當我編寫if(get_avatar()= 1)時,它不返回任何東西。網站在這種情況下停止工作。完整白頁僅 –

+0

我已經在使用此代碼。如問題所示。 –

+0

現在嘗試...更新代碼 – avktech