File format of post image attachment

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.

Read More

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?

Related posts

2 comments

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

Comments are closed.