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
- First, get the author ID by using get_the_author_meta(‘ID’)
- Get the Author Image URL by get_avatar_url($author_id)
- 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:
- First, get the author ID by using get_post_field(‘post_author’ , $post->ID) function
- Get the Author Image URL by get_avatar_url($author_id)
- 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.'"/>'; ?>