• Hello,

    I’m setting this variable in my header.php so I can grab the featured image associated with the page and display as the header background image.

    <?php $hero = wp_get_attachment_image_url( get_post_thumbnail_id($post->ID), 'full' ); var_dump($post->ID)?>

    <header class="<?php echo $class_names;?>" style="background-image: url('<?php echo $hero;?>');">

    It actually works great on my static home page template and other pages because I’m able to set the feature image on that page, but as soon as I set my “Reading Settings” for my “Posts Page,” my default blog page (index.php) attempts to grab the first post id, which is the ID (1), the first post that comes with a clean WordPress install, “Hello World.”

    I assume this is happening because after selecting your default blog page, the page locks out the option to select a featured image so it goes searching for an ID. With that said, should I create a template for my blog page so I have more control over what is displayed on the page? Is there a way to get the featured image to show up on the default blog page? I hope that makes sense. Thanks!

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • @get_username – The “featured image” functionality is generally based on a single page/post and needs to be able to identify that appropriate single value. $post->ID would be correct but does not accurately apply to archive type views in this specific case such as a blog index.php and/or other similar template pages such as a category.php, tag.php, archive.php, etc.

    You would need to write a more specific query to identify the first published post(?) and use its ID to designate the “featured image” for the blog index header (if I am understanding your idea here correctly).

    As you noted this works on page, I would also suspect your current code idea would work in single views of your posts, too.

    ~ Cais.

    Thread Starter get_username

    (@get_username)

    Thanks for the reply. I hadn’t thought about the other pages that this would happen on such as category.php, tag.php, archive.php, etc. I wonder if I’m approaching this correctly? Without hardcoding the “background-image” is there another way to loop into the featured image? I’m not sure what the more specific query is that you’re referring to. Do I need a custom template for my “blog” page or would that not work either because, like you said, there are other pages like archive.php. Thanks for any additional help you can provide.

    -Burt

    Thread Starter get_username

    (@get_username)

    It appears that using a template for blog posts is incorrect as pointed out here. I am setting all of my header background images in header.php, so I’m not sure how template hierarchy plays out here. If I use index.php or home.php they are still pulling from the same header.php file.

    Thread Starter get_username

    (@get_username)

    I just came across a post with this interesting code snippet.

    <?php if(is_home()) {
        $img = wp_get_attachment_image_src(get_post_thumbnail_id(get_option('page_for_posts')),'full');
        $featured_image = $img[0];
    }?>

    It looks like it gets the ID of the page you set under “Reading Settings.” I haven’t tested yet, but looks promising. I assume I would set the condition based on is_page(‘blog’) instead of home?

    Thread Starter get_username

    (@get_username)

    After many searches, guessing at what to search for, this solution works. I’d like if someone could check my logic. Is this the expected way to handle custom background images and custom fields on the Posts Page?

    For the background “Featured Image” on the Blog home page.

    <?php
        if (is_home()) {
            $hero = wp_get_attachment_image_url( get_post_thumbnail_id(get_option('page_for_posts')), 'full' );
        } else {
            $hero = wp_get_attachment_image_url( get_post_thumbnail_id($post->ID), 'full' );
        }
    ?>

    For the custom fields on the Blog home page.

    <?php
        if (is_home()) {
            the_field('header_headline', get_option('page_for_posts'));
        } else {
            the_field('header_headline');
        }
    ?>

    Could I have done this differently? Is there a standard? Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Setting “Posts Page” default no longer displays featured image’ is closed to new replies.