您的位置:首页»WordPress 教程, 主题开发»  使用 get_avatar 为 WP 自定义 Gravatar 默认头像

使用 get_avatar 为 WP 自定义 Gravatar 默认头像

在上一篇文章中我们谈到如何在 WordPress 中添加 Gravatar 默认头像,经雪深提醒,我翻了翻 codex,发现还可以用 get_avatar 函数指定默认 Gravatar 头像。
get_avatar 是 WordPress2.5 新增的一个函数,所以此方法只适用于 WordPress2.5 及以上版本,其基本用法如下:

1
2
3
<?php 
   echo get_avatar( $id_or_email, $size = '96', $default = '<path_to_url>' ); 
?>

id_or_email:必备参数,可以是一个 User ID(字符串),一个 email 地址,或是一个评论对象。Note:在绝大多数 WordPress 主题中,我们可以将 id_or_email 赋值为 $comment 或 get_comment_author_email(),用以显示评论者 Gravatar 头像,另外,我们也可以使用 get_the_author_id() 显示文章作者 Gravatar 头像。
size:可选参数,定义 Gravatar 头像大小,最大尺寸为512px,默认为96×96
default:可选参数,用以自定义 Gravatar 默认头像地址
如果您还在使用 WordPress2.5 以下版本,那么得做如下 hack:

1
2
3
4
5
6
7
8
   if (function_exists('get_avatar')) {
      echo get_avatar($email);
   } else {
      //alternate gravatar code for < 2.5
      $grav_url = "http://www.gravatar.com/avatar.php?gravatar_id=
         " . md5($email) . "&default=" . urlencode($default) . "&size=" . $size;
      echo "<img src='$grav_url'/>";
   }

好了,使用 get_avatar 函数为 WordPress 自定义 Gravatar 默认头像就介绍到这里。正所谓条条道路通罗马,您喜欢哪个方法呢?如果您有任何关于 WordPress 的小技巧,欢迎和站趣一起分享。

22个评论

  • Reply 1 LOKE

    March 13th, 2009 at 20:02

    我喜欢WP3.0!!希望那时候能直接上传就可以了。。。o(╯□╰)o

    • Reply 2 crossyou

      August 5th, 2010 at 09:49

      WP3.0已经出来了

      • Reply 3 LOKE

        August 5th, 2010 at 21:07

        哇,这位仁兄的回复真诡异~不过可惜我的愿望没实现,期待wp4.0!!

        • Reply 4 辐射鱼

          August 5th, 2010 at 21:34

          这就叫挖坟,哈哈哈,明儿新版上线

          • Reply 5 LOKE

            August 6th, 2010 at 09:33

            哈哈,恭喜恭喜,等我空间到期了,下次空间肯定搬你这儿!话说,怎么我看到的还是旧版?

            • Reply 6 辐射鱼

              August 6th, 2010 at 09:49

              调试首页的时候遇到bug了,郁闷

        • Reply 7 crossyou

          August 6th, 2010 at 16:57

          我想试下这位仁兄还健在不?

          • Reply 8 LOKE

            August 6th, 2010 at 17:02

            囧rz…你个mjj的

            • Reply 9 crossyou

              August 6th, 2010 at 19:08

              不要说脏话,开个玩笑。^_^

  • Reply 10 gao

    March 13th, 2009 at 22:56

    还是这种方法好啊,2.5以下基本上很少见了

  • Reply 11 yadandimao

    March 14th, 2009 at 11:36

    还没试过这种方法 一直用的是那个外国网上的

  • Reply 12 西风

    March 14th, 2009 at 17:01

    技术文章 看不懂也 坚定完毕

    • Reply 13 辐射鱼

      March 19th, 2009 at 11:18

      照着做就懂了,很简单的

  • Reply 14 aunsen

    March 14th, 2009 at 22:07

    来晚了,沙发没了!

    • Reply 15 辐射鱼

      March 19th, 2009 at 11:18

      你坐了那么多沙发,让一个出来不行么?嘿嘿

  • Reply 16 知更鸟

    April 1st, 2009 at 15:54

    这个弄走试试

  • Reply 17 leesum

    April 12th, 2009 at 12:31

    很想知道如何设置在评论里将作者与网友区分开来

    • Reply 18 辐射鱼

      April 12th, 2009 at 14:08

      具体方法根据你的 WordPress 版本而定,如果是2.7的话,可以直接在 css 里定义 bypostautho、byuser 的样式即可,其他版本稍微复杂一点点

  • Reply 19 大话西游

    October 30th, 2009 at 16:19

    在找一个可以自定义头像的插件,有什么好推荐的?

    • Reply 20 辐射鱼

      October 30th, 2009 at 18:57

      你需要些啥功能呢?头像不用上插件吧

  • Reply 21 also

    October 5th, 2010 at 13:28

    不懂将代码插入哪个位置好

    • Reply 22 辐射鱼

      October 5th, 2010 at 18:28

      一般是你主题的coments.php文件,找到显示头像的地方修改就是。