Wordpress - Get featured image on Blog Index
if you are referring to the 'page for posts', then try (only relevant section of your code shown):
if (is_home() && get_option('page_for_posts') ) {
$img = wp_get_attachment_image_src(get_post_thumbnail_id(get_option('page_for_posts')),'full');
$featured_image = $img[0];
} else {
I'd suggest anyone doing this consider the following adjustments:
- Get the ID of the current page/post/index using
get_queried_object()
. For a blog index that's set to a page this will return the correct page ID. - If all you want is the full-sized image use
wp_get_attachment_url()
instead ofwp_get_attachment_image_src()
Here's a quick function I'd use to achieve this in a simpler way:
/**
* Custom Featured Image
*/
function custom_featured_image() {
$queried_obj = get_queried_object();
// Don't use on single posts (JUST FOR THIS DEMO)
if ( is_single() ) return;
// Get the featured image ID
$image_id = get_post_thumbnail_id( $queried_obj->ID );
// Get the URL for the full sized image
$image_src = wp_get_attachment_url( $image_id );
return $image_src;
}
I personally like to avoid excessive nested conditional logic, using a function can help with this.