• The get_children code sample below returns an array of the images attached to a post:

    $images =& get_children( 'post_type=attachment&post_mime_type=image' );

    What I’d like to do is get the thumbnails of whatever photos are returned, using the dimensions set in Settings => Miscellaneous => Thumbnail size. Is there a clear way to do this? A function I can apply to the returned values?

Viewing 15 replies - 1 through 15 (of 20 total)
  • wp_get_attachment_image() and wp_get_attachment_image_src() in /wp-includes/media.php seems to do what you want (in WordPress 2.6.1, at least). Running the following code inside the loop will generate thumbnails of all images associated with the current post:

    $images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );
    foreach( $images as $imageID => $imagePost )
    	echo wp_get_attachment_image($imageID, 'thumbnail', false);

    You can use the array returned by wp_get_attachment_image_src() to create links instead of image tags, or check that the thumbnails are the correct size.

    BTW, the ‘post_parent’ argument needs passing to get_children() or you end up with an array of all the images in your database…

    What’s the easiest way to take the above code and limit the output to just one specifically sized image (i.e. 32×32)? Right now, the code outputs all the attached images at thumbnail size (150×150).

    Thanks!

    <?php $images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );
    $firstImageSrc = wp_get_attachment_image_src(array_shift(array_keys($images)));
    echo "<img src=\"{$firstImageSrc[0]}\" width=\"32\" height=\"32\" />"; ?>

    Stick the above code in the loop and that should do it. It grabs the first key from the $images array and then uses that to get the necessary url from wp_get_attachment_src().

    Note that this code only resizes the image using HTML, it doesn’t do anything to the thumbnail file itself. You’d need to change the size settings for thumbnails in /wp-admin/options-misc.php to change the image file dimensions, although that won’t change the size of existing thumbnails on your site, you’d need to fix those up manually if you want them changing.

    @mintimperial: Thanks for the code snippet, it is really useful.

    However, I need to test if there is an image attachment. If there isn’t, I want to display a default image. How do I do that?

    I insert the first code, with the thumbnail. I my localhost works, but when I put it in my server, doesn’t. I says: Warning: Invalid argument supplied for foreach() in /homepages/5/d238048801/htdocs/lafactoria.cl/wp-content/themes/lafactoriav3/category.php on line 13

    The line 13 is: foreach( $images as $imageID => $imagePost )

    Hope you can helpme

    Same problem here,
    Does anybody knows how to solve it ? thanks !

    What if you would like them to link to their attachment pages?
    I tried doing this from the image-upload, but they are still just dead (no links)

    do this code permit you to show the preview of the image in your post on the preview of the post?

    <?php $images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );
    foreach( $images as $imageID => $imagePost )
    	echo wp_get_attachment_link($imageID, 'thumbnail', false);    ?>

    This gets images and links to the image, but not the attachment page (unfortunately). Still asking how to do that.

    I got it linking with the article (using the second code, and inside the loop):

    <a href="<?php the_permalink(); ?>"><?php $images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );
    $firstImageSrc = wp_get_attachment_image_src(array_shift(array_keys($images)));
    echo "<img src=\"{$firstImageSrc[0]}\" width=\"150\" height=\"150\" />"; ?></a>

    I resized the image to 150×150 pixels here (to change in a custom way). Hope that helps.

    These are two different things, Dunkkan;
    – you are using this code on the index page to get the first post
    – I’m using a code on the single page to get all the images from a post, without displaying the text

    guys…
    i’m sorry i nedd some help

    here you see my website

    http://www.colazionedakiki.com which is in italian, but is not important.

    what i would like to do is to show the image in the post(you should click on it for see it) in the preview post on the homepage.

    which code i have to write and where?

    ….could some on advice me a good guide of php…i think i should start study hard!eheheheh

    Is it possible to use mintimperial’s method and get the medium image instead of the thumb? Also how can I attach a class to the image in this statement?

    Thanks

    I meant to say the second way with getting only one image…

    allmylove

    (@allmylove)

    @marklandry

    if you’re using Dunkkan’s method

    replace width=\"150\" height=\"150\"

    with your own desired height and width but note that it uses html to resize the images so if you’re blowing them up it could get fuzzy

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘Getting thumbnails of images attached to a post?’ is closed to new replies.