get_post_mime_type()

最后更新于:2021-11-26 22:34:18

get_post_mime_type( int|GC_Post$post=null)

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

参数

$post

(int|GC_Post) (Optional) Post ID or post object. Defaults to global $post.

Default value: null

响应

(string|false) The mime type on success, false on failure.

源文件

文件: gc-includes/post.php

function get_post_mime_type( $post = null ) {
	$post = get_post( $post );

	if ( is_object( $post ) ) {
		return $post->post_mime_type;
	}

	return false;
}
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).'" />';