+44 (0)785 715 1348
office@bogdans.co.uk

WordPress: get the featured image from the parent’s post 0

Posted on 11, September 2013

in Category Web development


Here’s what you need to do in order to get the image from the parent’s 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 : “” );

0 Comments