wp_get_attachment_thumb_urlを使う

wordpress3になってからアイキャッチ画像とかが設定できるようになったけれど、まだまだ本文中の画像をサムネイル化して、表示するという需要もあるわけで。

そんなときに少し便利な関数wp_get_attachment_thumb_urlの実装例です。

[php]
function get_thumb_url(){
$attachments = get_children(array(‘post_parent’ => get_the_ID(),
‘post_type’ => ‘attachment’,
‘post_mime_type’ => ‘image’,
‘orderby’ => ‘date’));
if ( is_array($attachments) ){
$first_attachment = array_shift($attachments);
$thumb_url = wp_get_attachment_thumb_url($first_attachment->ID);
return $thumb_url;
}
}
[/php]
その記事の一番古い添付ファイルのサムネイル画像のURLを返します。たぶん。。。。

wp_get_attachment_image_src()も似たような使い方ができますが、こちらはサイズをパラメーターとして渡さないとです。まぁこちらの方が便利な気がしますが・・・