I know you guys going to say this might be possible duplicate to PHP get index of last inserted item in array
But I've tried and not succeed to get my function work and I'm trying to figure it out why.
I've a function which will grab the thumbnail attachment from the content of Wordpress post.
The code working fine with a post that have only one image, but if there are more than one image in the post it will display the first image correctly from the array, but I don't know why it stores the image in reverse. Meaning the first image of the post stored last and the last store first. I'm geussing this is maybe how the media function of Wordpress work.
Anyway I just need to get the last inserted data from function even if it has one or more than one image stored in the array
// Get Image Attachments
function sa_get_image($postid=0, $size='thumbnail') {
if ($postid<1)
$postid = get_the_ID();
$thumb = get_post_meta($postid, "thumb", TRUE);
if ($thumb != null or $thumb != '') {
echo $thumb;
}
elseif ($images = get_children(array(
'post_parent' => $postid,
'post_type' => 'attachment',
'numberposts' => '1',
'post_mime_type' => 'image', )))
foreach($images as $image) {
$thumbnail=wp_get_attachment_image_src($image->ID, $size);
?>
<?php echo $thumbnail[0]; ?> // here is the problem where the image display.
<?php }
}