Question

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?

Was it helpful?

Solution

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".

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top