views:

671

answers:

3

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?

+2  A: 

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.

Doug Neiner
To clarify, can I use the new WP 2.9 "post_thumbnail" field as the image? Or does this solution require that I include the image within the post itself? -- For some reason, the above code isn't working for me, and I suspect it's because I haven't correctly "attached" the image.
Matrym
Ah, the image has to be uploaded onto that particular post, though it doesn't have to be inserted into the post body. I haven't used the new 2.9 feature yet, so I am not sure. This code works with 2.8+ as far as I know (its basically the same code I use on my site)
Doug Neiner
If you click on "Add Media" on the post edit screen, and click the Gallery Tab you should see at least one image. If you see no images, this code will not work.
Doug Neiner
So if the image was previously uploaded, it cannot be uploaded to this particular post? is that my problem?
Matrym
Right, if the image was previously uploaded using the generic "Media > Add New" page or uploaded to another post, this code won't work... sorry :(
Doug Neiner
Hrm, I just added a new image to the page using the on-page uploader and it's still not working... :(. Could you take a look at the code I just added/edited in my post?
Matrym
Sure, your code doesn't work because are passing in the `$post->ID` not the post **attachment** id. In my sample I am not *querying posts*, but querying for the *post's attachments*. They are stored in the same table as posts, but have a different post type of `attachment`. You have to first get the post attachment id (again, see my code) before you can call the `wp_get_attachment_thumb_url` or the `wp_get_attachment_image_src` functions. If you put my code in your functions.php file, you can just put `<?php get_attached_images() ?>` in place of your `echo wp_get_attachment_thumb_url...` code.
Doug Neiner
+1  A: 

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.

silent
<?php $paged = get_query_var('paged') ? get_query_var('paged') : 1; query_posts('category_name=projects ?> <?php while (have_posts()) : the_post(); ?> <?php echo wp_get_attachment_thumb_url( $post->ID ); ?> <?php endwhile; ?>
Matrym
Shouldn't the above comment work? For better legibility, I've copied it into an edited version of my post. I have an image in my "gallery" showing, so it is definitely attached.
Matrym
+1  A: 

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.

Matrym