画像ファイルの情報を取得する
2009年 5月 29日 admin
画像idからURIを取得
アップロード画像の各サイズ (thumbnail, medium, large, full) の URL は、アタッチメントの ID が分かれば wp_get_attachment_image_src( $id, $size ) で取得出来ます。
この関数は画像の URL と width, height を配列にして返します。
516 /** 517 * Retrieve img HTML content for an image to represent an attachment. 518 * 519 * @see wp_get_attachment_image_src() Returns img HTML element based on array. 520 * @since 2.5.0 521 * 522 * @param int $attachment_id Image attachment ID. 523 * @param string $size Optional, default is 'thumbnail'. 524 * @param bool $icon Optional, default is false. Whether it is an icon. 525 * @return string HTML img element or empty string on failure. 526 */ 527 function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false) { 528 529 $html = ''; 530 $image = wp_get_attachment_image_src($attachment_id, $size, $icon); 531 if ( $image ) { 532 list($src, $width, $height) = $image; 533 $hwstring = image_hwstring($width, $height); 534 if ( is_array($size) ) 535 $size = join('x', $size); 536 $html = '<img src="'.attribute_escape($src).'" '.$hwstring.'class="attachment-'.attribute_escape($size).'" alt="" />'; 537 } 538 539 return $html; 540 }
488 /** 489 * Retrieve an image to represent an attachment. 490 * 491 * A mime icon for files, thumbnail or intermediate size for images. 492 * 493 * @since 2.5.0 494 * 495 * @param int $attachment_id Image attachment ID. 496 * @param string $size Optional, default is 'thumbnail'. 497 * @param bool $icon Optional, default is false. Whether it is an icon. 498 * @return bool|array Returns an array (url, width, height), or false, if no image is available. 499 */ 500 function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = false) { 501 502 // get a thumbnail or intermediate image if there is one 503 if ( $image = image_downsize($attachment_id, $size) ) 504 return $image; 505 506 if ( $icon && $src = wp_mime_type_icon($attachment_id) ) { 507 $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/crystal' ); 508 $src_file = $icon_dir . '/' . basename($src); 509 @list($width, $height) = getimagesize($src_file); 510 } 511 if ( $src && $width && $height ) 512 return array( $src, $width, $height ); 513 return false; 514 }
Well, I’m not sure if there’s a better way, but this worked
$image=wp_get_attachment_image($num, 'large', false); $imagepieces = explode(" ", $image); $imagepath = substr($imagepieces[1],4);
In fact this is even cleaner, thanks to my buddies on Yay:
$image=wp_get_attachment_image($num, 'large', false); $imagepieces = explode('"', $image); $imagepath = $imagepieces[1];
Get ‘large’ image attachments inside loop?
エントリのidからその投稿に利用されている画像を探す。
/** 2487 * Retrieve attachment meta field for attachment ID. 2488 * 2489 * @since 2.1.0 2490 * 2491 * @param int $post_id Attachment ID 2492 * @param bool $unfiltered Optional, default is false. If true, filters are not run. 2493 * @return string|bool Attachment meta field. False on failure. 2494 */ 2495 function wp_get_attachment_metadata( $post_id, $unfiltered = false ) { 2496 $post_id = (int) $post_id; 2497 if ( !$post =& get_post( $post_id ) ) 2498 return false; 2499 2500 $data = get_post_meta( $post->ID, '_wp_attachment_metadata', true ); 2501 if ( $unfiltered ) 2502 return $data; 2503 return apply_filters( 'wp_get_attachment_metadata', $data, $post->ID ); 2504 } 2505
その他の情報については wp_get_attachment_metadata( $id ) で取得できます。
wp_get_attachment_imageでalt属性を出力できるようにした
another:
WordPress › フォーラム » the_attachment_linkで取得した画像をlightbox風に表示したい