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.

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:

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?

share|improve this question
    
Can I ask why you're using that code outside of WordPress? What exactly IS displayed/returned? –  Tom J Nowell Nov 21 '12 at 16:51
    
I don't want to make use of their template system, as I'm simply using the posts/categories of WP with another system. Nothing is returned/displayed other than "has a thumb" or "no thumb" which is accurate with each post. –  Braunson Nov 21 '12 at 16:53
    
Why not just use WordPress for everything? One can accomplish a vast number of things using WordPress without even touching the 'blogging' part. –  Tom J Nowell Nov 21 '12 at 16:58
    
It's a giant system, custom build and integrating it into WP would be a giant waste of time which is not in abundance right now as per this project. –  Braunson Nov 21 '12 at 17:22
    
i would stringly suggest calling wp-load.php instead of wp-blog-header.php. had a few problems lately with code using the latter. also, you have to echo the wp_get_attachment_image_src['0'], as this returns an Array, and does not print out the details. –  fischi Nov 21 '12 at 17:24

2 Answers 2

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.

share|improve this answer
    
Using your new code (with echo), returns nothing. With var_dump() returns this thumbstring(0) "". Odd since there is a set featured image for that post.. –  Braunson Nov 21 '12 at 17:22

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
share|improve this answer
    
The wp_die code above returns nothing at all, error_log returns nothing as well. –  Braunson Nov 21 '12 at 17:20
    
show me what the updated code I posted gives you ( using copy paste, not descriptive prose ) –  Tom J Nowell Nov 21 '12 at 17:27
    
Returns pastebin.com/mFghnp2n –  Braunson Nov 22 '12 at 16:44
    
can you do it again but this time put global $post; beforehand? –  Tom J Nowell Nov 22 '12 at 19:31
    
Done, exact same thing is returned as my pastebin... –  Braunson Nov 22 '12 at 20:21

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.