siteground
WordPress: Get author avatar without plugins

Showing avatars of post author is a very common and popular task. In WordPress, to retrieve avatar for post author, just easily use this code within Loop:

<?php echo get_avatar(get_the_author_meta('user_email'), 64); ?>

Change 64 to another size if you want. The function get_avatar has 4 parameters:

get_avatar($id_or_email, $size, $default, $alt);

where $default is url for an image, defaults to the “Mystery Man”; $alt is alternate text for the avatar.

In WordPress template, within Loop, to get author ID, use get_the_author_id() function (for WP 2.7 or lower, deprecated in WP 2.8); to get author email, use get_the_author_meta(‘user_email’) (for WP and later).

If you want to get avatar for comment author, use this code:

<?php echo get_avatar($comment, 64); ?>

Leave a Reply