Wordpress sites this as an example of how to get the thumbnail:

<?php echo get_the_post_thumbnail( $post_id, $size, $attr ); ?>

I want to know how can I use the same basic function but just have it automatically get the latest media upload's ID number instead of having to specify the post_id.


I don't think that you can use get_the_post_thumbnail function to get last uploaded media, but you can use get_post to get latest attachment and then wp_get_attachment_image to display images.

$attachments = get_posts( array(
    'post_type' => 'attachment',
    'posts_per_page' => 1,
    'post_status' => null,
    'post_mime_type' => 'image'
) );

foreach ( $attachments as $attachment ) {
    echo wp_get_attachment_image( $attachment->ID, 'thumbnail' );
  • The image is unattached to anything, so I was trying to get the most recently uploaded attachment / image and then go from there... – cwd Sep 14 '11 at 13:36
  • @cwd This snippet doesn't work for you? – Mamaduka Sep 14 '11 at 13:45
  • oh, hmm, maybe it would. i guess i read your intentions for what it would do wrong. let me give it a try :) – cwd Sep 14 '11 at 13:54
  • The code is good, just needs one tweak echo wp_get_attachment_image( $attachment->ID, 'thumbnail' ); - needs to include the ->ID bit. thanks. – cwd Sep 19 '11 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.