0
votes

I have a page on my Wordpress website called Blog, this is set to a static page and is the Blog page so it has a list of blog posts. On the blog page I have a featured image set. I want to use this featured image as a background image on the Blog page.

home.php (template for blog page):

At the top of home.php before the Loop that pulls the list of blog posts I have:

<?php
    $page_id = get_queried_object_id();
    if ( has_post_thumbnail( $page_id ) ) :
        $image_array = wp_get_attachment_image_src( get_post_thumbnail_id( $page_id ), 'optional-size' );
        $image = $image_array[0];
    else :
        $image = get_template_directory_uri() . '/images/default-background.jpg';
    endif;
    echo $image;
?>
<div class="feature" style="background-image: url('<?php echo $image; ?>')"></div>

This works and it sets the featured image. However it seems to have set the featured image on every single page on the website to the featured image from the Blog page. Even after I changed the featured image on the About Us page it still pulls the URL for the Blog page's featured image and sets that as the featured image on the About Us page.

I have the website here: http://www.cqwebdesign.co.uk/Action-Harpenden-Physiotherapy/

As you can see by these screenshots I have set different featured images on the Blog and About page:

http://i.imgur.com/DnU8V9F.jpg

http://i.imgur.com/qshulWT.jpg

Anyone know how I can fix this?

1
What does $page_id echo out to be on each page?Don Boots

1 Answers

0
votes

As the home.php template affects only your Blog Page, it would be interesting how your page.php template code looks like (Notice: page.php is the default template for pages like your 'About' Page) and why the featured image isn't shown correctly on this page(s).

Try to debug or echo your id values for the Blog Page and the About Page and verify if the corresponding page id is the same as the one referred by get_queried_object_id();.

I think you're having a page.php template but just in case, if you don't have one please notice Wordpress' Template Hierarchy

Wordpress Codex: When the static front page feature is configured properly, WordPress will not use a Custom Page Template to display the blog posts index, even if a Custom Page Template is assigned to the page designated as the "Posts page". WordPress will only use either home.php or index.php.