Created
June 13, 2013 01:26
-
-
Save iamkevingreen/5770562 to your computer and use it in GitHub Desktop.
Featured image of Parent page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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