DanL DanL -4 years ago 87
PHP Question

Blog page featured image being overridden by blog post featured image

On my Wordpress blog page (home.php) template, I'm trying to display the featured image set for that page at the top, then the content archive grid underneath. However, instead of displaying the featured image I set for the page, the first blog post's featured image is displayed instead. Additionally (related), if I remove the first blog post's featured image, it uses another post image on the content archives page. I'm guessing the two problems are related, but I'm not sure. Any help would be appreciated.

Here's the code I put in my functions.php

//Add featured images
add_action( 'genesis_before_content_sidebar_wrap', 'ws_post_image', 8 );
function ws_post_image() {
global $post;
wp_reset_postdata();
echo the_post_thumbnail($post->id); /*you can use medium, large or a custom size */
}

Answer Source

WordPress ignores anything you set on the "Page for Posts". Instead of treating it like a page in the general sense, it treats it more like an archive of posts and the "page" is merely a URL. That said, you can access the ID of the page and then use that to get the title, featured image, or other custom fields you assign to that page:

add_action( 'genesis_before_content_sidebar_wrap', 'ws_post_image', 8 );
function ws_post_image() {
    echo get_the_post_thumbnail( get_option( 'page_for_posts' ), 'large' ); // Or use whatever size you need
}

This says, "Get me the featured image on the page I set as the Blog page in Settings->Reading". The ID of that page is stored in your options table with the name page_for_posts.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download