Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

The code you posted is a function definition, and function definitions belong in functions.php to begin with. Simply move the code to functions.php, and then call it from wherever you need it in your template files.

If you want a variable image sizes, you simply need to pass an argument to the function. For example:

<?php
  function mytheme_get_attached_images( $size = 'full' ){
    // 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 ); ?>
      <img alt="<?php the_title(); ?>" src="<?php echo $img[0] ; ?>" width="<?php echo $img[1] ?>" height="<?php echo $img[2] ?>"/>
  <?php }
  }
?>

Then, when you call the function, it would look like this:

<?php mytheme_get_attached_images( 'medium' ); ?>

(Note: there are other ways the code could be improved; this just shows you how to use the existing code for your purposes.)

share|improve this answer
    
That made it, thank you so much Chip ;) –  javipas Aug 16 '11 at 14:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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