当前浏览:首页 / WordPress函数 / wp_get_attachment_metadata()

wp_get_attachment_metadata( int $attachment_id, bool $unfiltered = false )

检索给定ID的附件元数据

attachment 附件more...

metadatamore...


参数

$attachment_id

(int) (必填) 附件ID,默认为全局$post。

$unfiltered

(bool) (可选) 如果为true,则不运行筛选器。

默认值: false


返回

(array|false) 附件元数据(metadata),失败时为False。

  • 'width'
    (int) 附件宽度
  • 'height'
    (int) 附件高度
  • 'file'
    (string) 相对于 wp-content/uploads 的文件路径
  • 'sizes'
    (array) size的slug作为键,每个值是一个数组,包含 'file', 'width', 'height', 和 'mime-type'.
  • 'image_meta'
    (array) 图片元数据(metadata)
  • 'filesize'
    (int) 附件的文件大小


源码

查看源码 官方文档


更新日志

版本描述
2.1.0开始引入

使用示例

  • 示例1

    返回值示例

    Array
    (
    	[width] => 2400
    	[height] => 1559
    	[file] => 2011/12/press_image.jpg
    	[sizes] => Array
    	(
    		[thumbnail] => Array
    		(
    			[file] => press_image-150x150.jpg
    			[width] => 150
    			[height] => 150
    			[mime-type] => image/jpeg
    		)
    		[medium] => Array
    		(
    			[file] => press_image-4-300x194.jpg
    			[width] => 300
    			[height] => 194
    			[mime-type] => image/jpeg
    		)
    		[large] => Array
    		(
    			[file] => press_image-1024x665.jpg
    			[width] => 1024
    			[height] => 665
    			[mime-type] => image/jpeg
    		)
    		[post-thumbnail] => Array
    		(
    			[file] => press_image-624x405.jpg
    			[width] => 624
    			[height] => 405
    			[mime-type] => image/jpeg
    		)
    	)
    	[image_meta] => Array
    	(
    		[aperture] => 5
    		[credit] => 
    		[camera] => Canon EOS-1Ds Mark III
    		 => 
    		[created_timestamp] => 1323190643
    		[copyright] => 
    		[focal_length] => 35
    		[iso] => 800
    		[shutter_speed] => 0.016666666666667
    		[title] => 
    	)
    )
    
    
  • 示例2

    视频格式示例:

    array(10) {
    	["filesize"] => int(227431276)
    	["mime_type"] => string(9) "video/mp4"
    	["length"] => int(918)
    	["length_formatted"] => string(5) "15:18"
    	["width"] => int(1280)
    	["height"] => int(720)
    	["fileformat"] => string(3) "mp4"
    	["dataformat"] => string(9) "quicktime"
    	["audio"] => array(7) {
    		["dataformat"] => string(3) "mp4"
    		["codec"] => string(19) "ISO/IEC 14496-3 AAC"
    		["sample_rate"] => float(44100)
    		["channels"] => int(2)
    		["bits_per_sample"] => int(16)
    		["lossless"] => bool(false)
    		["channelmode"] => string(6) "stereo"
    	}
    	["created_timestamp"]=> int(-2082844800)
    }
    
  • 示例3

    请注意,当调用wp_get_attachment_metadata()时,从“file”返回的数组索引是相对于 wp-content/uploads 的文件路径。

    若你们想链接域名的url,可以使用下面的代码段

    $upload_dir = wp_upload_dir();
    $attachment_metadata = wp_get_attachment_metadata( $attachment_id );
    echo var_dump( $upload_dir['url'] . '/' . $attachment_metadata['sizes']['medium_large']['file'] );