How to get the Page featured image, not the Post featured image

I currently have in my header.php:

<?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'header-large' ); ?>

<?php if ( has_post_thumbnail() ) { ?>
<div id="wrapper-navbar" itemscope itemtype="http://schema.org/WebSite" style="background-image: url('<?php echo $backgroundImg[0]; ?>');">
<?php } else { ?>
<div id="wrapper-navbar" itemscope itemtype="http://schema.org/WebSite">
<?php } ?>

Which sets the background image if there is a featured image on the pages and posts. If there is none then there is a fall back where I have a default background image.

My problem arises on the Blog page - The page which is set as the "Posts Page'

The background image displayed on the "Posts Page" is the featured image of the latest Post on the list, NOT the featured image I have set for the page.

How do I display the featured image of the Page and not of the latest post on the list?

Solutions

You might be able to do this by calling wp_reset_postdata(); otherwise you will need to get the blog page ID and set that on the line before instead:

if ( is_home() ) {$post_id = get_option( 'page_for_posts' );} else {$post_id = $post->ID;}
$backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post_id), 'header-large' ); ?>

Note the is_home() is actually used for the blog page, as distinct from is_front_page() which is actually for the "homepage".

Similar questions

wordpress-theme 2017: featured image behaviour: where to set the height of the featured image?
good day dear community, after having nailed down several issues with the set up of the theme twentyseventeen - the question today is: wordpress-theme 2017: featured image behaviour: where to set the height of the featured image!? Well while i was doing a little google-search i have found the following; I have been searching on the whole net in ord...
outputting a featured image rather than a link to a featured image
In the past, I used this custom function to create an image carousel on my WordPress blog with Thesis theme It has been a while since I used the code, and, having recently setup a new WordPress blog, I discovered it doesn't work anymore, in that the image isn't getting displayed with the link to the post. Someone suggested to me that this might be ...
Wordpress Setting Featured Image Size and Adding link to featured image and Title
I've created a filterable portfolio in wordpress followindg a quicksand tutorial I found. Everything is working but the author did not link the featured image in the page template nor set the thumbnail size for the post type. I want the thumbnail size to be 280px x 180px. Here is the code: (Edited working code below)
Is there a way to pull the first featured image in a loop and not all other featured images?
I have a loop that pulls the first three posts of a category. I want to use the featured image of the first post pulled for the background of the section the titles and links to these posts are displayed. Is it possible to pull the first image without pulling all other images of posts that were pulled in the loop? Here's the current loop: EDIT (8/1...
How to get WordPress image thumbnail link? ( not featured image )
I am trying to build a simple gallery with WordPress Advanced Custom Fields and the repeater field plugin. I need to display a thumbnail image which you click to enlarge the main image (I am using Fancybox). Does anyone know whether it is possible to get the link to the image thumbnail WordPress automatically generates? I can get the main image lin...
Get featured image of post and link rel="preload" as="image" in wp-head
I have a code to put it in fuction.php. The code is working well. See below Currently, it is adding to header.php on entire website. But I want it to add in header.php only on archive pages and home page(set as latest post view). Thank You

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .