1. Mime Type of Attachments

    April 1, 2008 by jerome

    Today, during one of the soft redesign of Assembling I found myself in need to know whether an attachment was an image or not; the wordpress documentation did not give me any clue about how to do this – so I started to create myself the function I needed: get_the_attachment_type

    It sits in my theme functions.php – and might be of any use to someone else:
    <?php
    function get_the_attachment_type($id = 0) {
    $id = (int) $id;
    $_post = & get_post($id);
    $mime = $_post->post_mime_type;
    return $mime;
    }
    ?>

    I use the function in the attachment.php file in the following manner:


    <?php switch (get_the_attachment_type($post->ID)) {
    case "image/jpeg":
    case "image/gif":
    case "image/png":
    break;
    default:?>
    <p><b>Download</b>:
    <?php } ?>
    <?php echo $attachment_link; ?> <?php the_content(); ?>

    This will then output a bold Download prompt in front of the file’s name in case, the attachment file is not an image (pdf, zip, etc.); if the attachment is an image, then nothing will be displayed.