How To Add Gravatars For The Post Author in WordPress

Now that Gravatar support is part of the WordPress core adding them into your WordPress theme is easy. Adding them to your comments has been documented. How about adding them to your post titles to highlight the comment author? Within the loop? That’s fairly easy too. Here’s the code:

<?php echo get_avatar( get_the_author_email(), '80' ); ?>

Pretty simple, huh? get_the_author_email outputs the post author’s email and the “80” is the size of the avatar image in pixels (you can change that). How this will look depends on how you use it; where you put it in the theme (it has to be in the loop!) and how you style it.

Want an example? I had to figure this out for an upcoming project so…

Author Gravatar Example

Look at that frighteningly intense young man there in black and white. Good grief, it’s me! The author of the post! The code, it works! It works!

Ahem. Excuse me. I get a little excited about these things. Good luck using it in your own theme!

By the way, did you know WordPress has built in email protection from spam bots (read Disguising Your Email)? I didn’t. Neat.

Author: Ian Stewart

Canadian, designer, dad, and dork. I work at Automattic on .

162 thoughts on “How To Add Gravatars For The Post Author in WordPress”

  1. That’s pretty cool Ian. I enjoying reading your WordPress tips and your new Amazon bookstore looks cool. I gotta find some time to read your creative thinking recommendations.

  2. Cool! I love the idea. I’ve seen some sites with the author’s avatar right after the post but isn’t this gravatar thing more applicable in a multi-user blog?

  3. You’re totally talking about my latest theme. I implemented this technique in WPFreemium but instead of using the author’s email, I used their ID e.g. “the_author_ID();”.
    It took me hours trying to find out how this worked at first since I couldn’t find any documentation in the codex I had to dig into the core files.

  4. The theme you’ve come up with is a great example what I’ve outlined, Ptah. Very cool.

    And it’s totally frustrating when you know, in theory, you can do something and quickly realize you’re going to have to figure out how to do it yourself. Except of course when you figure it out. Then, well, that’s awesome. 🙂

  5. hai ian, can you point which specific place to put the code, because i have not found where the loop is? in comment.php, single.php or where?

  6. I still don’t understand exactly where i’m supposed to place the code. Where should i put it if i want it to look like in the picture? could you please show me?

  7. Where you put the code, Thomas, depends on the theme you’re using (remembering it has to be in The Loop) and how you style it, or position it, using CSS. Give it a try. It’s kinda fun.

  8. Ok thanks for the quick reply

    I really don’t know much CSS, but i just discovered that the theme i use, apparently has a single php file for most of the loop stuff… Im going to try to mess around with that, ive managed to do a lot by trial and error before…

    By the way do you have any idea how i can find the part of the code that shapes the post headline area? like specific terms to search for or something. I also need to change the color of the headlines themselves, but thats probaly somewhere else…

  9. Hey, how u did put the align=”right” only to the gravatar image?
    can u put me the code u did use to get that exact style?

Comments are closed.