2

OK, I got a custom post type with thumbnail enabled which adds the "featured images" panel to that post-type just fine, but in my template when I do

if ( has_post_thumbnail() ) {
    the_post_thumbnail();
} else {
    echo 'none';
}

it just shows 'none' on each one even though I have a featured image set... I have add_theme_support('post-thumbnails',array('post','custom_post_type')); so it's supported by the theme but has_post_thumbnail always returns false what's going on here???

My template is : archive-custom_post_type.php :

<?php
$loop = new WP_Query(array( 'post_type' => 'clients', 'posts_per_page' => -1 ));
if($loop){
?>
<ul>
<?php
while ( $loop->have_posts() ) : $loop->the_post();
?>
        <li>
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
            <?php 
                if ( has_post_thumbnail($post->ID) ) {
                    the_post_thumbnail('thumbnail');
                    echo get_post($post->ID)->post_excerpt;
                } else {
                    echo 'broken ::' . $post->ID;
                    echo get_post($post->ID)->post_excerpt;
                }

            ?>
            </a>
        </li>
<?php 
endwhile;
//wp_reset_postdata();
?>
</ul>
<?php
}
?>

3 Answers 3

3

In your Custom Post Type, do you setup_postdata( $post ) in your custom Loop? If not, has_post_thumbnail() might not be defined/available?

EDIT:

Try adding:

setup_postdata( $post );

Right before:

$loop->the_post();

And then see if has_post_thumbnail() returns true?

Or, try passing the $post->ID to your call to has_post_thumbnail()?

has_post_thumbnail( $post->ID );
7
  • No I dont, I've updated the question with the full page
    – JKirchartz
    Commented May 31, 2011 at 20:32
  • Then try adding setup_postdata( $post ); (I think, right before $loop->the_post()) Commented May 31, 2011 at 21:08
  • nope, still returning 'none'
    – JKirchartz
    Commented May 31, 2011 at 21:11
  • Is that both for using setup_postdata(), and for passing $post->IDto has_post_thumbnail()? Another question: are the_permalink() and the_title_attribute() working? Commented May 31, 2011 at 21:15
  • 2
    the_post() method calls setup_postdata() by itself, no need to add it.
    – Rarst
    Commented May 31, 2011 at 21:17
0

Try removing $post->ID from the has_post_thumbnail(). Perhaps the $post object isn't referencing your current post.

0

Add this at the top of your functions.php

if( ! function_exists( 'has_post_thumbnail' ) )
{
    function has_post_thumbnail( $pid )
    {       
        return get_post_meta( $pid, '_thumbnail_id', true );
    }
}
1
  • In the OPs case, has_post_thumbnail returned false incorrectly at all times. It didn't throw a Call to undefined function error. Please explain how your above code block would be of help to this issue. Commented Apr 16, 2013 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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