WordPress.org

Ready to get started?Download WordPress

Codex

Function Reference/get post mime type

Contents

Description

Retrieve the mime type of an attachment based on the ID.

This function can be used with any Post Type, but it makes more sense with Attachments.

Usage

<?php get_post_mime_type$ID ?>

Parameters

$ID
(integer) (optional) Post ID.
Default: ''

Return Values

Mime Type (boolean|string) 
False on failure or returns the mime type.

Examples

Return an icon image path according to the MIME type of the given post

function get_icon_for_attachment($post_id) {
  $base = get_template_directory_uri() . "/images/icons/";
  $type = get_post_mime_type($post_id);
  switch ($type) {
    case 'image/jpeg':
    case 'image/png':
    case 'image/gif':
      return $base . "image.png"; break;
    case 'video/mpeg':
    case 'video/mp4': 
    case 'video/quicktime':
      return $base . "video.png"; break;
    case 'text/csv':
    case 'text/plain': 
    case 'text/xml':
      return $base . "text.png"; break;
    default:
      return $base . "file.png";
  }
}
// call it like this:
echo '<img src="'.get_icon_for_attachment($my_attachment->ID).'" />';

Notes

WordPress already has a function to get the mime type icon called wp_mime_type_icon http://codex.wordpress.org/Function_Reference/wp_mime_type_icon

Change Log

Source File

get_post_mime_type() is located in wp-includes/post.php.

Related

See also index of Function Reference and index of Template Tags.