This code might be what you’re after. This loops through page templates to grab the featured image. If you add it outside the loop in your page.php
template (or index.php
, depending on your theme), it should grab the pages’ featured image, not the posts’.
<?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 beginning 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 : "" );
}
?>
You should add this outside your loop