I'm working on a website that can display 3D molecules (.mol) using the JavaScript-based viewer from JSmol (http://sourceforge.net/projects/jsmol/). With the "Upload File Type Settings" plug-in files with .mol extension can be uploaded as "Featured Image".

I managed to customize my single.php template with the JSmol script to display the .mol files.

Now I'd like to determine in the template if the featured image attachment of the post is an .mol (for displaying the 3D file via the script) or an image file format (JPG, PNG.. to display with the_post_thumbnail), such as:

<?php if ("the attachment has a .mol extension?") {
echo "<script>Jmol.getTMApplet('jmol', Info)</script>";
} else if {
} ?>

How can I find out which file format is used in the featured image attachment of the post?

2 Answers 2

$post_thumbnail_id = get_post_thumbnail_id();
$thumb = get_post($post_thumbnail_id);

Look at the post_mime_type in the $thumb object.




Maybe something like this? It saves the URL to the post's thumbnail (if set) in variable $src, then with a substring function you check what the last 4 letters are.

if ( has_post_thumbnail() ) {
    $src = wp_get_attachment_image_src( get_post_thumbnail_id(), 'post-thumbnail' );

    if( substr( $src, -4 ) == '.mol' ) {
        // it's a .mol extension
        // do your magic here
    } else {
        // it's something else, assuming a regular image
} ?>

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.