# Jesse Lawson

## Bestselling author and open-source evangelist.

Jun 24, 2013 - Tutorials

# Author Image as Default Post Image in WordPress

Someone in the WP.org support forums was looking for way to use my Easy Author Image plugin as a default post image when an author does not specify a post_thumbnail (featured image) for their article. Seems like an interesting idea, so I created a function to do it.

The function should do the following:

• Check to see if a post thumbnail exists
• If YES, get and return the URL
• If NO, check to see if the profile image has been uploaded
• If YES, get and return the URL
• If NO, continue
• Finally, if neither the post thumbnail or author image is set, get and return the default post image

Now that we have mapped out our requirements, let’s see the end result.

// Returns the URL of a post image, either the post thumbnail if the post
function get_post_image() {

global $post;$url = "";

if(has_post_thumbnail()) {  // Check to see if a post thumbnail exists
$url = get_the_post_thumbnail($post->ID);
} else if ( "" != get_the_author_meta('author_profile_picture') { // If there's no post thumbnail, use the author image
$url = get_author_image_url(); } else { // No post thumbnail and no author image, so use default image$url = get_bloginfo('template_directory') . "/images/post-default.jpg";
}

return $url; } Obviously you’ll want to change the ‘images/post-default.jpg’ to a default post image of your choice. I’ve added some comments to make it fairly straightforward. If you have additional questions you can either refer to the pseudo code above, the comments in the code, or the comments at the end of this article. Some example usage: // Somewhere in the loop, after while(have_posts()): the_post(); <?php$post_image = get_post_image(); ?>
<div class="post-image">
<img src="<?php echo \$post_image; ?>"/>
</div>
<div class="post-title">
<h1><?php the_title();?></h1>
</div>
<div class="post-content">
<?php the_content();
</div>

And that’s it.

Have you tried using this on your site? Did you make any modifications? Share your experience in the comments below!

Other tutorials about the author image: