I want to get the URL of a page’s “featured image”, as I want to use the page’s featured image as the background image for the banner at the top of the page. The background image for the banner changes according to what page I’m on, as they will have different featured images.
![](https://itnursery.com/wp-content/uploads/2022/04/apple-touch-icon@2.png)
3 s
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)
.