WordPress2.7は画像に「大サイズ」が加わって全部で4種類になりました。
記事から投稿された画像を出力する内容のエントリーはこれまでに何度か書いてますが、
wp_get_attachment_image() を利用して画像をゲットする関数を作ったので晒しておきます。
関連記事
functions.php
function get_the_post_image($postid,$size,$order=0,$max=null) { $attachments = get_children(array('post_parent' => $postid, 'post_type' => 'attachment', 'post_mime_type' => 'image')); if ( is_array($attachments) ){ foreach ($attachments as $key => $row) { $mo[$key] = $row->menu_order; $aid[$key] = $row->ID; } array_multisort($mo, SORT_ASC,$aid,SORT_DESC,$attachments); $max = empty($max)? $order+1 :$max; for($i=$order;$i<$max;$i++){ return wp_get_attachment_image( $attachments[$i]->ID, $size ); } } }
Usage
print get_the_post_image($post_id, $size, $order, $max);
- $post_id 画像を取得したい記事のID
- $size 取得したい画像のサイズ(thumbnail, medium, large, full )
- $order ギャラリーでの順序で入れてる数字。2にしたら2番目の画像
- $max $orderで入れた数字から何枚目の画像まで取得するか指定
普通画像を突っ込んだだけだと順序($order)は全部0なので、ギャラリーで画像の順序が指定されておらず
$order、$max共に指定しなかった場合は一番最後に投稿した画像が1枚だけ表示されるようになってます。
これはarray_multisortの2番目の配列に画像のIDをDESCで入れてるからですが、これをSORT_ASCにすれば最初に投稿した画像が表示されるようになります。
ループ内ならget_the_ID()が便利
print get_the_post_image(get_the_ID(),"thumbnail");
オプションで画像のサイズ変更しても変更したサイズで表示されます。
あんまり検証してないのでおかしいところがあったら教えて頂けると幸い。