WordPress 记录评论者 gravatar 头像
我已经数不清有多少朋友来问过我这个问题了,就是在 WordPress 评论框上的“欢迎 ** 归来”字样,以及评论者的头像,真是很不好意思,拖到现在才写这教程,不过其实关于“欢迎 ** 归来”的字样在之前的《记录并隐藏 WordPress 评论者信息》中已经详细地介绍过,因为本文介绍的记录评论者头像与此文相关,这里就不再解释重复和其中的 JQuery 部分了,我只重点介绍下关于记录并显示当然评论者头像的方法吧。也建议在看本文之前可以先了解一下《记录并隐藏 WordPress 评论者信息》中的原理,这样可以帮助你理解本文。
![]()
效果如上图,之前留过言的朋友也可以看我的评论框。代码相当简单,在 WordPress 主题中的 comments.php 中的昵称、邮箱、网址输入框上方添加如下代码:
<?php if ( $comment_author != "" ) : ?>
<?php echo get_avatar($comment_author_email,'32'); ?>
<div id="welcome">
<?php printf(__('欢迎 <strong>%s</strong> 归来! '), $comment_author) ?>
</div>
<?php endif; ?>
<?php echo get_avatar($comment_author_email,'32'); ?>
<div id="welcome">
<?php printf(__('欢迎 <strong>%s</strong> 归来! '), $comment_author) ?>
</div>
<?php endif; ?>
比较简单的一个判断,应该还是很好理解的,关键就是 WordPress 眼花缭乱的函数。
烦是涉及到 WordPress 头像的我都要扩展一下,那就是用头像缓存时的代码,关于 WordPress 的头像缓存可以先看看这篇:《Wordpress 2.7+主题 gravatar 头像缓存》,启用 WordPress 头像缓存后,你则可以试试以下代码,同样是在 Wordpress 主题中的 comments.php 中的昵称、邮箱、网址输入框上方添加如下代码:
<?php if ( $comment_author != "" ) : ?>
<img width='32' height='32' class='avatar' src='<?php echo '/gravatar/' . md5(strtolower($comment_author_email)) . '.jpg' ?>' alt='' />
<div id="welcome">
<?php printf(__('欢迎 <strong>%s</strong> 归来! '), $comment_author) ?>
</div>
<?php endif; ?>
<img width='32' height='32' class='avatar' src='<?php echo '/gravatar/' . md5(strtolower($comment_author_email)) . '.jpg' ?>' alt='' />
<div id="welcome">
<?php printf(__('欢迎 <strong>%s</strong> 归来! '), $comment_author) ?>
</div>
<?php endif; ?>
可以看到两者唯一的区别就在于头像的代码部分,其中 gravatar 头像缓存图片存放的路径就根据各自的实际情况修改啦。个人感觉是个不错的用户体验,大家可以试试。
-
Wordpress 侧边栏带头像的最新评论2009年12月26日 -
Wordpress 之欢迎 OOXX 归来2011年01月19日 -
Wordpress 评论数之排排坐吃果果2010年12月1日 -
给 Wordpress 评论框添加 TinyMCE 编辑器2010年10月3日