WordPress.org

Ready to get started?Download WordPress

Forums

Getting thumbnails of images attached to a post? (21 posts)

  1. matth
    Member
    Posted 6 years ago #

    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?

  2. mintimperial
    Member
    Posted 5 years ago #

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

  3. Jason Pelker
    Member
    Posted 5 years ago #

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

    Thanks!

  4. mintimperial
    Member
    Posted 5 years ago #

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

  5. bechster
    Member
    Posted 5 years ago #

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

  6. mantra.el
    Member
    Posted 5 years ago #

    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

  7. Dunkkan
    Member
    Posted 5 years ago #

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

  8. Lars M
    Member
    Posted 5 years ago #

    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)

  9. Alberto_Campora
    Member
    Posted 5 years ago #

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

  10. Lars M
    Member
    Posted 5 years ago #

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

  11. Dunkkan
    Member
    Posted 5 years ago #

    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 150x150 pixels here (to change in a custom way). Hope that helps.

  12. Lars M
    Member
    Posted 5 years ago #

    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

  13. Alberto_Campora
    Member
    Posted 5 years ago #

    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

  14. marklandry
    Member
    Posted 5 years ago #

    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

  15. marklandry
    Member
    Posted 5 years ago #

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

  16. allmylove
    Member
    Posted 5 years ago #

    @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

  17. tdmohr
    Member
    Posted 5 years ago #

    This is great, almost exactly what I was looking for.

    Is it possible to manipulate mintimperials 2nd method, so that instead of just returning the first thumbnail, it excludes it, showing only the 2nd, 3rd, 4th etc?

    Thanks

    Tim

  18. tdmohr
    Member
    Posted 5 years ago #

    Hello,

    Still trying to figure this out.

    I tried changing "<img src=\"{$firstImageSrc[0]}\" />"; ?>
    to
    "<img src=\"{$firstImageSrc[1]}\" />"; ?> which to me should pull the next entry out of the array.

    Obviously I don't understand arrays enough. Any help?

    Tim

  19. paul2008
    Member
    Posted 5 years ago #

    Hello everyone,

    please help me with the error I get (similar to other posters').

    I pasted mintimperial's code into my page.php, right after the beginning of posts loop:

    if (have_posts()) : while (have_posts()) : the_post(); ?>

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

    I get 2 errors:

    Warning: array_keys(): The first argument should be an array in /home/www/mywebsite.com/wp-content/themes/basic2col/page.php on line 11

    Warning: array_shift(): The argument should be an array in /home/www/mywebsite.com/wp-content/themes/basic2col/page.php on line 11

    Line 11 being
    $firstImageSrc = wp_get_attachment_image_src(array_shift(array_keys($images)));

    Is this because $images is actually empty?
    And then is &images empty because it searches for pics in posts on the current page (and current page is empty, has no posts)?

    If correct, then how do I make it search through all posts in the blog, not only on the current page?

    Thanks for support.

  20. paul2008
    Member
    Posted 5 years ago #

    A quick update:

    I tested the same code on index.php
    It displays those 2 error messages right after each post's excerpt.

    What am I doing wrong?

    Thank you for help.

  21. paul2008
    Member
    Posted 5 years ago #

    Just tried to
    print "images[0] is $images[0] !";

    and it prints "images [0] is !"
    So, $images is empty, I suppose.

    I don't know how get_children and all the other stuff involved here works, so I can't understand why is nothing written in $images.

    Could you explain it a little?

    Thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic