1

I am working on a site and each page group has its own featured image. I am trying to do a conditional statement in the header but not sure how to check for the parent page's featured image.

<?php 
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
  the_post_thumbnail();
} else{?>

    <img src="<?php bloginfo('template_url')?>/images/BANNER-DEFAULT.jpg"/>


<?php
}
?>

I want it to preform like:

<?php 
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
  the_post_thumbnail();
} 

if ( ?????? { // check if the parent page has a Post Thumbnail assigned to it.
  the_post_thumbnail();

}else{?>

    <img src="<?php bloginfo('template_url')?>/images/BANNER-DEFAULT.jpg"/>


<?php
}
?>

Can anyone help with this? I am also considering calling in the banner on the page templates and not the header, but I still need a way to call in the featured image on the Parent Page.

1 Answer 1

4

I believe you want something like the following:

<?php 
if( has_post_thumbnail( $post->post_parent ) ) { 
  echo get_the_post_thumbnail( $post->post_parent );
}
?>

Calling the_post_thumbnail(); should return the current post thumbnail, whereas using get_the_post_thumbnail(); will allow you to specify the ID, using the parent ID in your case.

Function Reference for get_the_post_thumbnail()

Also, if you're doing this outside of the loop, you'll need the $post global variable, as seen here.

1
  • Thanks! Works great
    – OnengLar
    Jun 22, 2020 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.