The thumbnail is stored on the PDF attachment, is it possible that you are trying to get a thumbnail for a normal post?
An easy way to test if thumbnails are working is to add the code you posted to single-attachment.php and open a PDF-attachment to verify that thumbnail is shown.
You can also use get_the_post_thumbnail($pdf_attachment_id)
if you know the attachment ID.
If this does not work, I need some more information to go on, viewing the entire template file in would be helpful.
I’m also having trouble bringing this into my template file.
The thumbnail is created and is in the media library, but I cannot get it to show. I can’t see any reference anywhere to a link between the attachment file (the PDF) and the thumbnail that has been created.
I have tried the following code in my template file: I am using the Advanced Custom Fields plugin, and my custom field for the PDF is called ‘file’ hence the get_field('file');
<?php $attachment_id = get_field('file');
$filethumb = get_post_thumbnail_id( $attachment_id ); ?>
<div class="thumb"><?php the_post_thumbnail( $filethumb ); ?></div>
I have lots of other elements on the template using the $attachment_id
and it is returning the id of the PDF file (which i’m using for file names, file sizes etc)
I’m not sure how to go about testing it as you have mentioned above. My theme does not have a single-attachment.php file…
You mention that it works as the featured image, which I have a meta box for on my edit page – should the thumbnail be being displayed here?
This looks to be a super sweet plugin, worthy of many stars : )
A subtle detail of the_post_thumbnail()
is that it only works with current post in The Loop, you cannot pass an ID to that function. Your code should work if you replace the_post_thumbnail
with echo get_the_post_thumbnail
:
<?php echo get_the_post_thumbnail($filethumb); ?>
Thanks, but unfortunately still nothing displaying for me if I use get_the_post_thumbnail()
I have got it working through using this method:
<?php $attachment_id = get_field('file');
$filethumb = wp_get_attachment_image_src( get_post_thumbnail_id( $attachment_id ), medium ); ?>
<div class="thumb"><img src="<?php echo $filethumb[0] ?>"/></div>
wp_get_attachment_image_src
got me everything I needed 🙂
Now a very happy chap!
Great! Thank you for posting the solution, and a great review.
Actually, get_the_post_thumbnail($id)
calls wp_get_attachment_image_src($id)
internally, so you should be able to make it work with the code below. In my earlier comment, $filethumb
should have been $attachment_id
, sorry for the blunder.
<?php $attachment_id = get_field('file'); ?>
<div class="thumb">
<?php echo get_the_post_thumbnail($attachment_id, 'medium'); ?>
</div>
That would make your code slightly easier to read.