wordpress – how to show first attachment

add this to your theme’s “functions.php” page:


function list_first_image() {
$files = get_children('post_parent='.get_the_ID().'&post_type=attachment&post_mime_type=image');
if($files) :
$keys = array_reverse(array_keys($files));
$j=0;
$num = $keys[$j];
$image=wp_get_attachment_image($num, 'large', false);
$imagepieces = explode('"', $image);
$imagepath = $imagepieces[1];
$thumb=wp_get_attachment_thumb_url($num);
print " '$thumb' ";
endif;
}

alternatively you can also pass along some size requirements. this is the one that’s in use under each ‘single post’ on www.myconfinedspace.com :

function get_first_image() {
$files = get_children('post_parent='.get_the_ID().'&post_type=attachment&post_mime_type=image');
if($files) :
$keys = array_reverse(array_keys($files));
$j=0;
$num = $keys[$j];
$image=wp_get_attachment_image($num, 'large', false);
$imagepieces = explode('"', $image);
$imagepath = $imagepieces[1];
$thumb=wp_get_attachment_thumb_url($num);
print "";
endif;
}

once you have either of those added to your functions.php file, you can call them using :

or

Leave a Reply