-3

How to show file type of featured image in post on single.php?

For example: Featured image type is: ".jpg"

I can get mime-type, like "image/jpeg".

It could be also some check of image mime-type first, for example if image mime-type is "image/jpeg" then echo ".jpg" if it is "image/gif" echo ".gif".

2
  • 1
    Now people are answering the question in two different places on the same site. Please read How to Ask asap.
    – kaiser
    Commented Jul 1, 2014 at 15:53
  • 1
    Also this is close to a duplicate of a question that you asked a year ago. Why did you not follow up on the old one?
    – kaiser
    Commented Jul 1, 2014 at 15:56

1 Answer 1

2

If you want to show the File type of featured image. then try the below code.

<?php
    $id1 = get_post_thumbnail_id($post->ID);
    $type =  get_post_mime_type( $id1 );
    $mime_type = explode('/', $type);
    $type = '.'.$mime_type['1'];
    echo $type;
?>

It will display the exact extension that you want ".jpeg" from "image/jpeg"
Here "$post->ID" is current post id (or the id of the post whose featured image you want ). Check if it works for you.

5
  • Thank you, it works. Just one note, it shows ".jpeg" for image which is ".jpg"? For other extensions like ".jpeg", ".gif", ".png" it shows exact extension. Commented Jul 2, 2014 at 8:45
  • Yes, it will work for all. I choose one just for example Commented Jul 2, 2014 at 8:49
  • Yes, it works for all extensions. Is it possible for you to make it show ".jpg" for ".jpg" file, instead of ".jpeg" for ".jpg" file? Commented Jul 2, 2014 at 8:57
  • It will return ".jpg" from "image/jpg" and ".jpeg" from "image/jpeg", ".png" from "image/png". Commented Jul 2, 2014 at 9:32
  • When I upload image with extension ".jpg" it returns ".jpeg". Also I checked with echo get_post(get_post_thumbnail_id())->post_mime_type and I get "image/jpeg" for image with just ".jpg" extension. Any idea why? Commented Jul 2, 2014 at 9:40

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.