1

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 {
the_post_thumbnail();
} ?>

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

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

Look at the post_mime_type in the $thumb object.

Reference

http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail
http://codex.wordpress.org/Function_Reference/get_post

1

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.

<?php
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
        the_post_thumbnail();
    }
} ?>

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.