# WordPressで投稿の画像を取得

- リンク: https://torounit.com/blog/wordpress%e3%81%a7%e6%8a%95%e7%a8%bf%e3%81%ae%e7%94%bb%e5%83%8f%e3%82%92%e5%8f%96%e5%be%97/
- 公開日: 2011-10-25T10:33:02+09:00
- 作成者: Toro\_Unit

WordPressで投稿の画像だけを表示することはアーカイブページやトップページでよくある
と思います。

たとえば[こんな感じ](http://getnews.jp/)

なので、それ用の関数をつくりました。
 投稿サムネイルがある場合はそれを、ない場合
は記事のギャラリーに入っている一番最初の画像を表示しています。

[php]
 function print_post_image($size = ‘thumbnail’){ if ( has_post_thumbnail()){
the_post_thumbnail($size); } else { $attachments = get_children(array( ‘post_parent’
=> get_the_ID(), ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image’, ‘order’
=> ‘DESC’ ));

 if(!empty($attachments)){

 $img = array_shift($attachments);
 echo wp_get_attachment_image($img->ID ,$size);}}}[/
php]

the_excerpt等と一緒に使うと結構楽しいかもしれません。
