Skip to main content
null 💻 notes

Author Image as Default Post Image in WordPress

Someone in the 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:

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 class="post-title">
 <h1><?php the_title();?></h1>
<div class="post-content">
 <?php the_content(); ?>