Last active
October 10, 2015 05:07
-
-
Save BFTrick/3637935 to your computer and use it in GitHub Desktop.
This script looks for a featured image on the current page, if it can't find a featured image it will continue to look backwards through the family tree until it can find one. Then it will print it.
This file contains 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 : "" ); | |
} |
@rachelbaker Thanks for the tip! I don't know why I didn't see this notification earlier but I appreciate it. :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Don't forget to check that $post has been set.
global $post
if ( isset($post) ) {
// your code here
}