1

I've been brought in to help out with development on an existing plugin, and have run into a strange issue. Not sure if I'm overlooking something obvious here.

The situation: I'm adding a shortcode that will basically spit out a list of posts of a particular custom type, complete with title/link/thumbnail. Standard stuff. The below code is giving me trouble.

        if( has_post_thumbnail() ) {
            $recipes .= get_the_post_thumbnail();
        } 

The $recipes variable is returned at the end of the function, to be output with the corresponding shortcode.

Edit with more info: The above code is within The Loop, with a custom query limiting retrieved posts to just those of the particular cpt.

All other post info is outputting correctly, but neither get_the_post_thumbnail() or the_post_thumbnail() return anything. There is a thumbnail attached to these posts in admin, has_post_thumbnail() returns true, and even get_post_thumbnail_id() works as expected. The thumbnail is definitely there.

I tried changing my query to get regular posts, rather than the custom post type, and thumbnails suddenly worked fine, so it has something to do with the cpt. I've tried making $post global, and even manually adding a post id as a parameter. No effect.

At the moment I'm just using wp_get_attachment_url( get_post_thumbnail_id() ) as a workaround but it's driving me crazy not knowing what's wrong.

What ridiculous mistake have I made?

Things I've tried:

  • Disabling all other plugins, changing to default theme(s)
  • Manually entering a valid cpt post ID as the parameter to get_the_post_thumbnail()
  • global $post; and using the ID from that
  • Try passing in the post ID to get_the_post_thumbnail($post_id, 'thumbnail'); and specify the image size you want. Check out these examples in the WordPress Codex. – Michael Ecklund Sep 25 '13 at 12:42
  • Thanks, but as mentioned above I've already tried adding in the post id as a parameter with no result. Just double checked it with the size specified too, no change. – Shellbot Sep 25 '13 at 12:45
  • You'll need to post more context. Saying a function doesn't work without any context of how it's used doesn't give us much to go on. Most WordPress functions are not deterministic: calling the_post_thumbnail in different places on the page with the same inputs may yield different results, for instance. Depends on global state, etc. – chrisguitarguy Sep 25 '13 at 12:48
  • @Shellbot Double check the post in question indeed does have a featured image set. Then check the results of has_post_thumbnail($post_id); is it accurate? – Michael Ecklund Sep 25 '13 at 12:49
  • @MichaelEcklund It's actually all posts of the particular cpt type, rather than one in particular, but yes I've double checked that a thumbnail is set and has_post_thumbnail($post_id) definitely returns true. As I said, even get_post_thumbnail_id() is working so there's definitely a thumbnail there. – Shellbot Sep 25 '13 at 12:53
2

If nothing works, give this a try:

$img_attribs = wp_get_attachment_image_src( get_post_thumbnail_id(), 'thumbnail' ); // returns an array
if( $img_attribs ) {
?> 
<img src="<?php echo $img_attribs[0]; ?>" width="<?php echo $img_attribs[1]; ?>" height="<?php echo $img_attribs[2]; ?>">
<?php } ?>
1

I had the same problem and I could not make get_the_post_thumbnail() work. However I found a workaround to display the thumbnail on custom type, just adding the code below.

<?php echo wp_get_attachment_image( get_post_thumbnail_id(), 'thumbnail' ); ?>

Posting in case it may be helpful.

1

Since you are trying to get the thumbnail within the loop like home page, category page or any other term page. you have to use - the_post_thumbnail();

If you want it in a specific page like single page, you should be using get_post_thumbnail_id($postId);

  • Just a tip, use backticks( on my keyboard, it is the ~ button) or the {} sign above the editor for code, don't use bold lettering :-) Thank you – Pieter Goosen Feb 7 '15 at 7:03
  • Another tip: you can print backticks by using Alt+96 (numbers are typed on the numeric keyboard, not the one above the letters) – Erenor Paz Mar 22 '17 at 8:58
1

I have the same problem, but that solved:

$attachment_id = get_post_thumbnail_id($post->ID);
$image = wp_get_attachment_image($attachment_id, 'thumbnail');
-1

Called from the loop, this worked for me:

get_the_post_thumbnail( get_the_ID() );
  • get_the_ID() is redundant. It will use the current post in the loop by default. – Jacob Peattie Jul 24 '18 at 6:44
  • @JacobPeattie that's because calling get_the_post_thumbnail() without an argument returns an empty string, as the question so clearly states. Including the "redundant" argument makes it work, for me at least. This solution is one related to the question, not a generic one. – che-azeh Jul 25 '18 at 13:32
  • get_the_ID() gets the id from literally the exact same place as the default argument for get_the_post_thumbnail() does: the global post variable. If one works but not the other then there’s a different more serious issue that should be fixed. Using get_the_ID() like this in that situation would be a hack, not a fix for the issue. – Jacob Peattie Jul 25 '18 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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