How to display a page’s featured image?

Adapted from this thread on the WP forums:

<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'single-post-thumbnail'); ?>
<style>
  #banner-id {
    background-image: url('<?php echo $image[0]; ?>');
  }
</style>
<?php endif; ?>

Add this to your single page template, after the_post(). I’d reccommend having a default header image so that if the page doesn’t have a featured image it falls back to using that.

'single-post-thumbnail' can instead be an array with the ideal header dimentions, such as array(600, 30).

Leave a Comment