WordPress 程式的評論頭像是自動的呼叫全球 gravatar 頭像,程式本身是不支援使用者設定頭像的,網站後臺只支援 「對於那些沒有自定義頭像的使用者,您可以顯示一個通用頭像或者根據他們的郵件地址產生一個頭像。」 網站管理員可以設定一些 「小怪物頭像」 。

如果你自己要設定自己頭像,必須使用你的郵箱到 gravatar 網站上去設定。隨著 gravatar 網站被國內遮蔽,即使你設定了個性頭像,也沒法在自己網站上顯示出來。
為瞭解決這個問題,有些人安裝了某些頭像外掛來輔助實現設定功能,而今天分享給大家的這個功能是透過判斷使用者的等級去顯示他的頭像。
- WordPress 網站把使用者分為 5 個等級,分別為:管理員、編輯、作者、投稿者、訂閱者,當然不要忘記給沒有註冊的遊客也做一張頭像圖片。我們首先需要對這 6 個使用者的使用者各自建立一個頭像圖片,圖片的大小為 48px*48px 。
- 將這 5 張圖片分別取名為 1.jpg,2.jpg,3.jpg,4.jpg,5.jpg ,6.jpg,並把它們放到網站主題資料夾下的 images 資料夾。
- 將以下函式放到自己網站的模板函式 functions.php 中;
[php]//評論 判斷管理員
function is_admin_comment( $comment_ID = 0 ) {
$comment = get_comment( $comment_ID );
$admin_comment = false;
if($comment->user_id == 1){
$admin_comment = true;
}
return $admin_comment;
}
//評論 判斷編輯
function is_editor_comment( $comment_ID = 0 ) {
$comment = get_comment( $comment_ID );
$editor_comment = false;
if($comment->user_id == 1){
$editor_comment = true;
}
return $editor_comment;
}
//評論 判斷作者
function is_author_comment( $comment_ID = 0 ) {
$comment = get_comment( $comment_ID );
$author_comment = false;
if($comment->user_id == 1){
$author_comment = true;
}
return $author_comment;
}
//評論 判斷投稿者
function is_Contributor_comment( $comment_ID = 0 ) {
$comment = get_comment( $comment_ID );
$Contributor_comment = false;
if($comment->user_id == 1){
$Contributor_comment = true;
}
return $Contributor_comment;
}
//評論 判斷訂閱者
function is_subscriber_comment( $comment_ID = 0 ) {
$comment = get_comment( $comment_ID );
$subscriber_comment = false;
if($comment->user_id == 1){
$subscriber_comment = true;
}
return $subscriber_comment;
}[/php]
以上函式是用來判斷網站文章的評論者是哪一個等級。
- 將以下的程式碼放到自己網站的評論模板 comments.php 中,查詢一下評論模板中顯示頭像的程式碼。 (含有 gravatar 的字樣)
[php]
<?php
if (is_admin_comment($comment->comment_ID)){?>
<img src="<?php bloginfo('template_directory'); ?>/images/1.jpg" width="48px" alt=" 管理員頭像"/>
<?php } elseif (is_editor_comment($comment->comment_ID)){?>
<img src="<?php bloginfo('template_directory'); ?>/images/2.jpg" width="48px" alt=" 編輯頭像"/>
<?php } elseif (is_author_comment($comment->comment_ID)){?>
<img src="<?php bloginfo('template_directory'); ?>/images/3.jpg" width="48px" alt=" 作者頭像"/>
<?php } elseif (is_Contributor_comment($comment->comment_ID)){?>
<img src="<?php bloginfo('template_directory'); ?>/images/4.jpg" width="48px" alt=" 投稿者頭像"/>
<?php } elseif (is_subscriber_comment($comment->comment_ID)){?>
<img src="<?php bloginfo('template_directory'); ?>/images/5.jpg" width="48px" alt=" 訂閱者頭像"/>
<?php } else {?>
<img src="<?php bloginfo('template_directory'); ?>/images/6.jpg" width="48px" alt=" 遊客頭像"/>
<?php }?>[/php]
- 這樣當有使用者在網站上釋出評論時,網站程式就會自動判斷使用者的級別,並且顯示設定的相應的頭像了。
