Random thumbnail images from media gallery

<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'orderby' => 'rand'
);
$attachments = get_posts($args);
$noimages = count($attachments);
 
if ($attachments) {
 
foreach ($attachments as $attachment) {
$alttxt = $attachment->post_title;
$imgid = $attachment->ID;
$fileurl = $attachment->guid;
 
$meta = wp_get_attachment_metadata($imgid);
$imgw = $meta['sizes']['thumbnail']['width'];
$imgh = $meta['sizes']['thumbnail']['height'];
 
$imgext = substr($fileurl, -4);
$fileurl = substr($fileurl, 0, -4);
$fileurl = $fileurl."-".$imgw."x".$imgh.$imgext;
 
// construct the image
echo "<img src='".$fileurl."' alt='".$alttxt."' class='alignleft highlightimg' />";
break;
}
}
the_excerpt('');
?>

Revisions

There are no revisions for this post.

Tags: , , ,

No comments yet.

Leave a Reply