2

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 }

}
2
  • 1
    In your post, the word reserve was supposed to be reverse? Commented May 3, 2012 at 13:47
  • Damn me! Sorry I'm going to edit that now :D Commented May 3, 2012 at 13:48

3 Answers 3

2

change:
$thumbnail=wp_get_attachment_image_src($image->ID, $size);
to:
$thumbnail[] = wp_get_attachment_image_src($image->ID, $size);

don't forget to declarate $thumbnail = array() outside the for-each.

and then you can use (preferred):

echo end($thumbnail);

or something like:

echo $thumbnail[count($thumbnail)-1];

If you wan't to get the 'last' appended value.
Or you can use [0] or first() to get the first value


You can add it here (ex.):

$thumb = get_post_meta($postid, "thumb", TRUE); 
$thumbnail = array(); //<<<
if ($thumb != null or $thumb != '') {
Sign up to request clarification or add additional context in comments.

4 Comments

Where can I declare the $thumbnail = array() ??
Well @Daan Timmer This is already my completed function though, as I mentioned above that without trying to get the last index it will works fine, so I'm assuming that it just some problem with the logic?
Unaccepted? You are obviously calling your function from somewhere. Thus it is not your complete code that interacts with this function/method
I'm sorry but I tried with your solution and it doesn't work. and this is the whole code I have and when I ask they said there is an array (you can look at my code) which actually control the order.
1

You are accessing $thumbnail as an array but you are overwriting the variable inside your foreach() as if it was a string.

Change:

$thumbnail=wp_get_attachment_image_src($image->ID, $size);

to

 $thumbnail[]=wp_get_attachment_image_src($image->ID, $size); 

to make it an array then the rest of your code should work

AH

4 Comments

print_r() the output of wp_get_attachment_image()
I'm not really good with php is this how you do it? print_r ($wp_get_attachment_image() );
Youll need to pass the values too print_r(wp_get_attachment_image_src($image->ID, $size));
still I can't see anything :/
0

There is an easier method to my solution and here is how we can get the last from the array.

Simply just add this to the array area.

'orderby' => 'menu_order',
'order' => 'ASC', //If reverse use DESC

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.