Featured Image as Header Image with Fallback

If you want to add the featured image as header image, you can use the following snippet (in this example, the Genesis Framework is being used):

function purewp_add_header()
{
	// Skip, if post type is not page
	if (get_post_type() != 'page') {
		return;
	}
	
	// Define images size
	$image_size = 'header-image';
	
	// Get image
	$image = get_the_post_thumbnail(null, $image_size);
	
	// If image is not set => Fallback
	if (!$image) {
		global $post;
		
		// Get parent page (if there is one)
	    $ancestors = get_ancestors( $post->ID, 'page' );
	    $parent_id = $ancestors[0];
		
		// Check if parent page has a featured image
	    if( has_post_thumbnail( $parent_id ) ) {
			// Use featured image of parent page
			$image = get_the_post_thumbnail($parent_id, $image_size);
	    }
	}
	echo '<div id="header-image"><div class="image">'. $image .'</div></div>';
}

add_action('genesis_after_header', 'purewp_add_header');