How to Get an Author Image in WordPress

To get the author image, you can use the get_avatar_url() function by passing author id as an argument. This will return the image URL of the author.

There are two examples given below to get the author image:

  • Get Author Image by Author ID
  • Get Author Image by Post ID

Get the Author Image by Author ID

  1. First, get the author ID by using get_the_author_meta(‘ID’)
  2. Get the Author Image URL by get_avatar_url($author_id)
  3. Display the author image

You can find the code given in the example below.


<?php

// Get the author ID	
$author_id = get_the_author_meta('ID');

// Get the author image URL	
$output = get_avatar_url($author_id);

// Display the author image	
echo '<img src="'.$output.'"/>';

?>

Get the Author Image by Post ID

You can also get the author image by post id, all you have to do is follow the steps:

  1. First, get the author ID by using get_post_field(‘post_author’ , $post->ID) function
  2. Get the Author Image URL by get_avatar_url($author_id)
  3. Display the author image

You can find the code given in the example below.


<?php

global $post;

// Get the author ID	
$author_id = get_post_field('post_author' , $post->ID);

// Get the author image URL	
$output = get_avatar_url($author_id);

// Display the author image	
echo '<img src="'.$output.'"/>';

?>

Reference

Owner of diveinwp.com, solely responsible for creating helpful, informative content about WordPress, a fulltime WordPress developer with 8+ years of hands-on experience in WordPress design and development.