gravatar是Globally Recognized Avatar的缩写,是 http://www.gravatar.com 推出的一项服务,意为“全球通用头像”。
如果在Gravatar的服务器上放置了你自己的头像,那么在任何支持Gravatar的blog或者留言本上留言时,只要提供你与这个头像关联的email地址,就能够显示出你的Gravatar头像来。
gravatar图像路径结构:
http://www.gravatar.com/avatar/3b3be63a4c2a439b013787725dfce802?s=80&r=g&d=identicon
参数说明:
3b3be63a4c2a439b013787725dfce802:根据 Emial 生成的 16进制的Email hash代码。(必须提供此参数)
s=80:指定图像的大小。默认值为80px,范围为1到512像素。
r=g:等级
- G — 普通级别,任何年龄的访客都适合查看
- PG — 有一定争议性的头像,只适合13岁以上查看
- R — 成人级,只适合17岁以上成年人查看
- X — 最高等级,不适合大多数人查看
d=identicon:默认图像
- 神秘人- Mystery Man
- 空白 – Blank
- Gravatar标志 – Gravatar Logo
- 抽象图形(自动产生) – Identicon (Generated)
- Wavatar(自动产生) – Wavatar (Generated)
- 小怪物(自动产生) – MonsterID (Generated)
在 WordPress 控制面板中设置相关参数。
在 WordPress 的文章或评论中显示用户图像代码:
1. 我们在functions.php添加下面的函数,以便在文章中直接调用。
function get_recent_comment()
{
global $wpdb; $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author,comment_author_email, comment_date_gmt, comment_approved, comment_type,comment_author_url,SUBSTRING(comment_content,1,80) AS com_excerpt
FROM $wpdb->comments
LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID)
WHERE comment_approved = '1' AND comment_type = '' AND
post_password = ''
ORDER BY comment_date_gmt DESC
LIMIT 5";
$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
foreach ($comments as $comment) {
$output .= "\n<li><img src=\"http://www.gravatar.com/avatar/".md5($comment->comment_author_email). "&s=42&d=&&r=G' alt=\"".strip_tags($comment->comment_author)." />\n
<a href=\"" . get_permalink($comment->ID) .
"#comment-" . $comment->comment_ID . "\">" . strip_tags($comment->com_excerpt)
."</a></li>";
} $output .= $post_HTML;
echo $output;
}
代码说明:
- md5($comment->comment_author_email) :将关联的email转化为16进制 hash md5 码。
- 可根据需要自行修改上述函数。
2. 调用get_recent_comment()函数,在需要放置 最新评论 的地方放置如下代码。
<ul>
<?php echo get_recent_comment(); ?>
</ul>
3. 输出样式:
如本站右侧部分 最新评论 部分。
那我看看我的头像。
不错
学习了,不错
学习了,原来这个还可以设置的啊.