Two years ago in StackOverflow there was an interesting question about using specific thumbnails, small, medium or large images on different parts of the site. The question, available here, was well answered by one of the users, Doug Neiner, who proposed to use the following code:
<?php
function get_attached_images(){
// This function runs in "the_loop", you could run this out of the loop but
// you would need to change this to $post = $valid_post or something other than
// using the global post declaration.
global $post;
$args = array(
'post_type' => 'attachment',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$attachment = get_posts($args); // Get attachment
if ($attachment) {
$img = wp_get_attachment_image_src($attachment[0]->ID, $size = 'full'); ?>
<img alt="<?php the_title(); ?>" src="<?php echo $img[0] ; ?>" width="<?php echo $img[1] ?>" height="<?php echo $img[2] ?>"/>
<?php }
}
?>
But that is not what I exactly need, because this code has to be inserted in every loop on my index.php or single.php (for example) to work.
Could it be possible to create that code on the functions.php and then call that code from each loop? This would be smarter and more elegant, I guess, because using all that code everytime on each loop seems a bit inelegant...
Are there other ways to do this?