Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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 { 
share|improve this answer
    
Awesome! That worked. Thanks a lot Michael –  demogar Sep 10 '12 at 1:55

You have 2 quick options, via the template file using the_post_thumbnail for the loop. I assume your outputting the data in a typical blog format and thus your function above won't work or act very weird inside the loop.

Instead try something like this in the actual template file where your main loop is (maybe index.php or loop.php) :

  //loop starts
   if ( has_post_thumbnail() ) { 
   the_post_thumbnail();
   }
   //the_content(); and other stuff
   //loop ends

Or if you want to use an action instead to alter the main loop you can use pre_get_posts, for example in your functions.php file.

Something like:

add_action( 'pre_get_posts', 'add_featured_image' );

function add_featured_image( $query ) {

    if( $query->is_main_query() && $query->is_home() ) {
        //your image code
    }

}

Notice that the above is checking 2 parameters, the main query and the home page, it is very important to check if it is the main query, otherwise it will alter all queries.

Reference:http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.