如何让你的theme支持WordPress 2.5的Gravatars

Posted in WordPress / 软硬一本 @ Mar 19th, 2008 by underone |

之前我说过,一直在等WordPress 2.5,为的就是准备内置gravatar的支持。现在2.5RC1我装上了,如你们看到的,G7v5这个theme已经支持了gravatar。如果你也做好了为2.5升级的准备,并且想放弃任何一种gravatar插件,那么看下边儿……

首先需要打开后台的gravatar支持:进入后台(/wp-admin),选择settings—reading,将Avatar display选项选择为show Avatar,如图即可。

Avatar display

这个选项应该是默认开着的。接下来,当然你的theme应该还是不会有什么变化——当然了,缺一些代码。记住我们永远的学习对象:default theme,任何新的例子都应该在里面可以找到,我就是这么找到的……

如果已经升级到2.5,理论上……首先应该屏蔽之前使用的gravatar插件(不管哪种插件),同时在comments.php里删除之前添加的相关代码。完成以后就是个纯净的comments.php了,然后开始。

在想要显示gravatar的位置(我一般插入到<?php comment_text() ?>附近)插入如下代码:
<?php echo get_avatar( $comment, 32 ); ?>
32是你想要显示的头像的大小,数字自己改。完成这一步以后保存comments.php,刷新,肯定会出来个头像。
我不懂php,在之前的本地测试的theme中我是这么写的:
<?php echo get_avatar( get_comment_author_email(), '32' ); ?>
貌似也是正常的……(来路)。

但是这个头像显然没有任何修饰,对吧?但是css是万能滴!以上代码会输出一个.avatar的class。如果知道如何修改那就动手去。如果不知道……可以用任何文本编辑器打开theme目录中的style.css文件,在任何一行添加以下代码(这只是我使用的代码而已,牛人不要笑!):

.avatar {
 border: 1px solid #d3d3d3;
 background-color: #ffffff;
 padding: 2px;
 float: right;
 margin-top: 3px;
}

这事儿到此为止,应该就差不多了……如果你讨厌默认的那个默认头像,你也可以自定义。这么写:
<?php echo get_avatar( $comment, '32', 'http://www.你的网址.com/你的头像.jpg' ); ?>
我觉得该这么写……但是相对路径不知道怎么写才能搞出来,有知道的回复给我啊:)

延展阅读

47 Responses to “如何让你的theme支持WordPress 2.5的Gravatars”

  1. Alan  Reply to this comment

    好文, 受教了 :D

  2. 锐风  Reply to this comment

    啊, 真是太强悍了..

  3. wind  Reply to this comment

    学习了。

  4. reb  Reply to this comment

    我只是想说说话!

  5. 3dsdesk  Reply to this comment

    学习了

  6. Dreamcolor  Reply to this comment

    收了!这两天我也去改改我用的那个主题。

  7. My Blog » Blog Archive » 升级到WordPress 2.5 RC1  Reply to this comment

    [...] 更标准的方法,谢谢Underone,如何让你的theme支持WordPress 2.5的Gravatars [...]

  8. iakuf  Reply to this comment

    8X8X 会了

  9. iakuf  Reply to this comment

    .......................呵呵新主题什么时候出来

  10. underone  Reply to this comment

    个把礼拜吧……还在检查整理

  11. evannan  Reply to this comment

    我是来测试头像的=。=

  12. evanWu  Reply to this comment

    这个也是

  13. July's Blog » Blog Archive » 升级到WordPress 2.5 RC1  Reply to this comment

    [...] 关于内建Gravatars的使用:(以下内容转自Underone)如果已经升级到2.5,理论上……首先应该屏蔽之前使用的gravatar插件(不管哪种插件),同时在comments.php里删除之前添加的相关代码。完成以后就是个纯净的comments.php了,然后开始。 [...]

  14. Amos  Reply to this comment

    嗯,也來試試頭像。

  15. Spring79  Reply to this comment

    有两个问题:
    一,我设置了css,不知道为何不起作用。
    二,请问,发文的人在哪里设置自己的头像呀?

  16. Michael  Reply to this comment

    按上面代码,怎么把绝对地址换成相对地址呢?

  17. underone  Reply to this comment

    @Michael
    理论上应该是
    '< ?php bloginfo('stylesheet_directory'); ?>/images/xxx.jpg'
    这是相对当然theme的images目录

    @Spring79
    怎么不起作用法……你看我这里不是好的……是不是class的名字写错了?
    and,设置要在http://en.gravatar.com/
    这里

  18. 小谋深算  Reply to this comment

    g7 v2的代码应该放在哪里?

  19. underone  Reply to this comment

    < ?php comment_text() ?>的旁边
    comments.php里

  20. 聿歆  Reply to this comment

    我也是测试头像的。。 呵呵

  21. underone  Reply to this comment

    你们。。。
    都跑我这测个啥。。。。

  22. z.Yleo77  Reply to this comment

    2.5支持ie6怎么样????

  23. tsian  Reply to this comment

    支持ie是theme的事情,和wodpress版本啥关系?

  24. underone  Reply to this comment

    同楼上。。。

  25. Jing  Reply to this comment

    出现了~出现了~偶也RC了。。。

  26. underone  Reply to this comment

    好神奇。。。为啥RC2了反而头像选项没了。。

  27. 蓝染大人  Reply to this comment

    测试一下

  28. 沙风  Reply to this comment

    让我们一起来使用avatar...

    在写这篇日志前我从没使用过avatar。
    avatar在英文中是“化身”的意思,其实简单通俗地说就是“个人头像”。
    我想我们对个人头像一定很熟悉,这还用说吗?我们在论坛里使用个人头像...

  29. 陈默  Reply to this comment

    学习了。

  30. justk  Reply to this comment

    同测

  31. levon  Reply to this comment

    学习来了。。。HOHO

  32. levon  Reply to this comment

    为什么我照这个做了,在我的博客却不出现头像呀

  33. underone  Reply to this comment

    我在你的博客试验了,出现了啊
    http://ilevon.cn/index.php/2008/04/09/create-a-link-page/

  34. levon  Reply to this comment

    没有啊,,,我没看到你的留言呀。晕咯,闹鬼啦

  35. levon  Reply to this comment

    饿,,我犯了一个不该饭的错误。ADMIN的邮箱填错啦,哈哈
    笨死了我

  36. 香草与叹息  Reply to this comment

    @levon
    @underone
    有一种可能,如果你使用的是firefox,并且安装了ad block插件,gravatar会被屏蔽掉。
    关了ad block,就看见了。

  37. underone  Reply to this comment

    @levon
    hahaha

  38. 二手科学家  Reply to this comment

    谢谢,学习了

  39. interjc  Reply to this comment

    好多新东西 ,我都想换theme了

  40. leo  Reply to this comment

    测试一下头像

  41. July's Blog » 升级到WordPress 2.5 RC1  Reply to this comment

    [...] 关于内建Gravatars的使用:(以下内容转自Underone) 如果已经升级到2.5,理论上……首先应该屏蔽之前使用的gravatar插件(不管哪种插件),同时在comments.php里删除之前添加的相关代码。完成以后就是个纯净的comments.php了,然后开始。 [...]

  42. Gxgz  Reply to this comment

    对这个Gravatar还是不大明白...

  43. ameter  Reply to this comment

    学习啊了

  44. iYangyi  Reply to this comment

    如何改默认的头像啊,没有账号的怎么办?

  45. underone  Reply to this comment

    要去gravatar.com注册啊

  46. pigpot  Reply to this comment

    不知道我的有没有 试试

  47. Joe  Reply to this comment

    哈,终于把我的头像加上去了。去看看我的。

Leave a Reply ↓