Get featured image on Blog Index

I’ve been trying to use the “featured image” from the home (the blog index) with no luck. It’s working for every single page, but it’s not working for the home.

The code looks something like this:

// Don't use on single posts
    if (!is_single()) {
        if (is_home()) {
            if (function_exists('wp_get_attachment_thumb_url')) {
                $img = wp_get_attachment_image_src(get_post_thumbnail_id(),'full');
                $featured_image = $img[0];
            }
        } else {
            if (function_exists('wp_get_attachment_thumb_url') && has_post_thumbnail()) {
                $img = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'full');
                $featured_image = $img[0];
            }
        }
        if ($featured_image) { ?>
            // A lot of code...
        <?php }
    }

I already tried to obtain the thumbnail using the _thumbnail_id meta. Same result.

This code is placed on the functions file, I believe that the issue is that it’s trying to get the loop/posts featured image.

Thanks a lot in advance.

5 s
5

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 { 

Leave a Comment