Skip to content

Instantly share code, notes, and snippets.

@iamkevingreen
Created June 13, 2013 01:26
Show Gist options
  • Save iamkevingreen/5770562 to your computer and use it in GitHub Desktop.
Save iamkevingreen/5770562 to your computer and use it in GitHub Desktop.
Featured image of Parent page
<?php global $post;
if (isset($post)) {
//get the ancestors
$familyTree = get_ancestors($post->ID,'page');
array_unshift( $familyTree, $post->ID ); //add the current page to the begining of the list
//loop through the family tree until you find a result or exhaust the array
$featuredImage = '';
foreach ( $familyTree as $family_postid ) {
if ( has_post_thumbnail( $family_postid ) ) {
$featuredImage = get_the_post_thumbnail( $family_postid, 'full' );
break;
}
}
// if the page has a featured image then show it
echo ( $featuredImage ? $featuredImage : "" );
}?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment