WordPress - get_post_mime_type

Wordpress - get_post_mime_type

我在具有 mime 类型的页面上显示文件列表。

    $type = get_post_mime_type( $document['document'] ); 

对于 pdf,它输出 'application/pdf'

是否可以删除应用程序并只显示 pdf。

您可以对字符串进行简单的 explode() 操作:

$type = explode( '/', get_post_mime_type( $document['document'] ) );
echo $type[1];

或者编写自定义函数以进行更细粒度的控制(基于 Codex example):

function so28344776_get_mime_for_attachment( $post_id )
{
    $type = get_post_mime_type( $post_id );

    if( ! $type )
        return 'N/A';

    switch( $type )
    {
        case 'image/jpeg':
        case 'image/png':
        case 'image/gif':
            return "image";

        case 'video/mpeg':
        case 'video/mp4': 
        case 'video/quicktime':
            return "video";

        case 'text/csv':
        case 'text/plain': 
        case 'text/xml':
            return "text";

        case 'application/pdf':
            return "pdf";

        default:
            return "file";
    }
}

// usage
echo  so28344776_get_mime_for_attachment( $document['document'] );