<?php
$id = $post->ID;
if( ! has_post_thumbnail( $id ) ) {
// the current page has no feature image
// so we'll see if a) it has a parent and b) the parent has a featured image
$ancestors = get_ancestors( $post->ID, 'page' );
$parent_id = $ancestors[0];
if( has_post_thumbnail( $parent_id ) ) {
// we'll use the parent's featured image
$id = $parent_id;
}
}
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($id), 'full' );
?>
<div class='guideHeader' style='background-image: url(<?php echo $thumb['0']; ?>);'>
The result is that, if the current page has a featured image set, that will be used. If it doesn't, but it does have a parent page, then we'll check to see if that parent has a featured image. If it does, we'll use that page's ID
to get the featured image. If the parent page doesn't have a featured image, then we'll just use the current page's ID
and let the chips fall where they may.
This is quick and dirty, and does no real error handling. (A glance at the Codex pages for get_post_thumbnail_id()
and wp_get_attachment_image_src()
indicates that they'll both return false
if there's no featured image, which means that in the worst case your page won't have a background image.)
References
has_post_thumbnail()
andget_ancestors()
(the page's immediate parent will be the zeroth element of the array thatget_ancestors()
returns).