Monday, February 7, 2011

WordPress Post List With Thumbnails

The function bellow, shows a list with posts and custom size thumbnails(for the post first image), linked to each post permalink. First copy this code and paste it in the function.php in your theme directory.

function thumb($id)
{
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $id
);
$attachments = get_posts($args);
// here we set the variable for the attachment string
$imgsrc = "";
if ($attachments):
// here we take the first image and break the loop
foreach ($attachments as $attachment) {
// this brings the attachment array $imgobj = wp_get_attachment_image_src($attachment->ID);
// if you use the line obove, you can call $imgobj[0] to get the image source
// you can set the thumbnail dimensions, here we use 40 x 40
$imgsrc = wp_get_attachment_image($attachment->ID, array(150,100), $icon = false);
break;
}
endif;
return $imgsrc;
}


Call the function in your index.php or home.php ..

<a href="<?php the_permalink() ?>"><?php echo thumb($post->ID); ?></a>


That's all done..

1 comment:

  1. Wonderful guide.. ideal for a site I'm building at this moment thank you!I saw in the most recent wordpress 3.0 the crate 'Post Thumbnail" is currently alluded to as 'Featured Image'…however regardless it lives up to expectations the same.
    ,,,,,,,,,,,,,,,,,,,,,,,,,,
    have a peek here

    ReplyDelete