You can use the recursive function as follows:
function get_featured_recursive($post) {
if (has_post_thumbnail( $post->ID ) ) {
return $post->ID;
} else if (!has_post_thumbnail($post->ID) && $post->post_parent != 0) {
$parent = get_post($post->post_parent);
if (has_post_thumbnail($parent->ID)){
return $parent->ID;
}
} else if(!has_post_thumbnail($parent->ID) && $parent->post_parent != 0){
get_featured_recursive(get_post($parent->post_parent));
}
else if(!has_post_thumbnail($parent->ID) && $parent->post_parent == 0 )
{
return null;
}
}
Then in your template use this to display the image:
< ?php $featured_image_post = get_featured_recursive($post);
if ($featured_image_post != null) : $featured_image_src = wp_get_attachment_image_src(get_post_thumbnail_id($featured_image_post), 'single-post-thumbnail'); ?>
source
share