Matrym
Matrym

Reputation: 17053

Wordpress: wp_get_attachment_thumb_url

There seems no way of referring to the "big size" "mid size" or "small size" image in a theme. Compounding that problem is their naming convention of 100 x ??? file name format, preventing hard coding a reference. Does anyone know of any solutions?

Upvotes: 1

Views: 7770

Answers (3)

Matrym
Matrym

Reputation: 17053

Ok, for everyone's future reference... using WordPress 2.9's new thumbnail feature, you can specify different image sizes like so:

<?php the_post_thumbnail('thumbnail'); ?>
<?php the_post_thumbnail('medium'); ?>

etc.

Sigh. This is one of those "Duh" moments that I imagine everyone searching and finding this page will come to.

Doug Neiner and Silent, thank you both very much for your contributed thoughts and answers. I'll +1 for your efforts, but it turns out that the answer was simpler than we all thought.

Upvotes: 1

ariefbayu
ariefbayu

Reputation: 21979

to use wp_get_attachment_thumb_url:

<?php
echo wp_get_attachment_thumb_url( $post->ID );
?>

NOTE: You MUST use the snippet above, inside the loop, so that you can get $post variable.

Upvotes: 2

Doug Neiner
Doug Neiner

Reputation: 66191

The function you want to use is wp_get_attachment_image_src, but it starts with passing it an id of a valid attachment id. Here is an example of how to pull back the first attachment on a post, and it tries to order it by "menu_order" which is the order set when you reorder items in the "Gallery" tab of the "Add Media" popup:

<?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 }
  }
?>

The important thing to notice is you can pass in "thumbnail", "medium", "large" and "full" which correspond to the same sizes in the Add Media box. Also, it returns an array:

[0] => url
[1] => width
[2] => height

EDIT: You can edit which sizes are created by WordPress by customizing them under "System->Media" in the WordPress backend.

Upvotes: 4

Related Questions