0

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?

1 Answer 1

1

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.)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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