get_post_mime_type() - WordPress 函数参考中文文档

返回介绍

get_post_mime_type()

发布于 2017-09-10 字数2602 浏览 929 评论 0

get_post_mime_type( int|WP_Post $ID = '' )

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.


参数

$ID

(int|WP_Post)
(Optional)
Post ID or post object.

Default value: ”


返回值

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


源代码

File: wp-includes/post.php

function get_post_mime_type( $ID = '' ) {
	$post = get_post($ID);

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

	return false;
}

更新日志

Version 描述
2.0.0 Introduced.

相关函数

Uses

  • wp-includes/post.php:
    get_post()

Used By

  • wp-admin/includes/image-edit.php:
    wp_image_editor()
  • wp-admin/includes/image.php:
    wp_generate_attachment_metadata()
  • wp-includes/post-template.php:
    get_body_class()

User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by Codex

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!