Issue where WP Featured Image will not display

The code I’m using, is outside of WordPress i.e.

define('WP_USE_THEMES', false);
require('blog/wp-blog-header.php');    
global $wpdb;

The code that I’m trying to display the post thumbnail (the set featured image) is:

Read More
if(has_post_thumbnail($post->ID)){
    echo "has a thumb";
    get_the_post_thumbnail();
    the_post_thumbnail();
    the_post_thumbnail('medium');
    wp_get_attachment_image_src(get_post_thumbnail_id());
    wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ));
} else {
    echo "no thumb";
}

Now it does does correctly display if there is a thumb or not, but doesn’t return the thumb file name or anything at all. I’ve tried all methods I could find and nothing is returned, nor is there any errors returned.

Suggestions, thoughts?

Related posts

Leave a Reply

3 comments

  1. Let’s examine the differences between the working and the not-working code:

    • Working: has_post_thumbnail( $post->ID )
    • Not Working: get_the_post_thumbnail();

    The only difference I see is that you’re passing the $postID explicitly to the working function, and not passing it to the non-working function. So, try this:

    get_the_post_thumbnail( $post->ID, 'medium' );
    

    You can var_dump() or echo it to verify whether or not you’re getting the correct data returned.

  2. get_the_post_thumbnail, and wp_get_attachment_image_src return their output, they don’t echo it out.

    In this case to get the url for a given size:

    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ),'thumbnail');
    $url = $image[0];
    $width = $image[1];
    $height = $image[2];
    

    it may be noted that the thumbnail size will be used if none is specified but I’ve added it to be precise

    If you’re in need of further clarification on exactly what is being returned by those functions, and want something stark and outright blatantly visible, I’d suggest something such as:

    $value = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ));
    wp_die('<pre>'.print_r($value,true).'</pre>');
    

    Though I would recommend you use error_log rather than wp_die and look at the error log in a text editor, as wp_die is not something you want in production code

    edit: -_- debug all the things!!

    ?>
    <pre>
    post ID : "<?php $post->ID; ?>"<br>
    has_post_thumbnail : "<?php if(has_post_thumbnail($post->ID)){ echo 'true'; }else {echo 'false'; } ?>"<br>
    thumb ID : "<?php get_post_thumbnail_id( $post->ID ); ?>"<br>
    thumb ID null? : "<?php if(get_post_thumbnail_id( $post->ID ) == null) { echo 'yes'; } else { echo 'no'; } ?>"<br>
    wp_get_attachment_image_src : "<?php print_r(wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID )),false)?>"
    </pre>
    <?php