How to: Use Thumbnails Generated by WordPress

One of ten brilliant tips that I shared yesterday, display images on your blog’s homepage without any custom fields or any additional functions.php script, something I first saw on WebDeveloperPlus.

How do you do it? First log in, on the sidebar select ‘Media’ (which is under ‘Settings’). You’ll then be taken to a page with an option to change the thumbnail size of images. Change that to whatever size you want your images to appear as. Next, insert the code below onto your homepage, archive page, whatever.

<?php
//Get images attached to the post
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => -1,
'order' => 'ASC',
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
$img = wp_get_attachment_thumb_url( $attachment->ID );
break; }
//Display image
} ?>

Then, to display your image you can just echo out the $img tag we just created:

<img src="<?php echo $img; ?>" alt=" " />

  • Leave a Comment