WordPress.org

Ready to get started?Download WordPress

Forums

Image thumbnails (15 posts)

  1. storpappa
    Member
    Posted 5 years ago #

    I want a picture in a post - easy - use the built in media uploader. Now step #2 - if I do this, is there an easy way to pull the image out to make a post thumbnail?

    I am looking for a more 'automatic' way to add a thumbnail of an image on the summary and archieve pages. Drop in a set of DIV tags to scale the image down to a set size, and the call to bring the image out of the post. Or if it has multiple images, just the first image.

    I'll have to keep looking - I am sure someone has done this already and even added default images by tag or categories to make it even more dynamic.

    I have seen this done in many themes by using custom fields. In fact, setting of different WordPress sites for friends, families and projects I see a mix of options for images. Custom fields for features, favorites, thumbnails. And even some theme authors change their approach from version to version. I don't see a consistent best practice or standard way to do this.

  2. @mercime
    Volunteer Moderator
    Posted 5 years ago #

  3. Michael Fields
    Themer
    Posted 5 years ago #

    You can do it by 'hand' too:

    <?php
    $images = get_children(
    array(
    	'post_parent' => $post->ID,
    	'post_status' => 'inherit',
    	'post_type' => 'attachment',
    	'post_mime_type' => 'image',
    	'order' => 'ASC',
    	'orderby' => 'menu_order'
    	)
    );
    
    if ( $images ) {
    foreach ( $images as $id => $image ) {
    	$img = wp_get_attachment_thumb_url( $image->ID );
    	$link = get_permalink( $post->ID );
    	print "\n\n" . '<div><a href="' . $link . '"><img src="' . $img . '" alt="" /></div>';
    }
    }
    ?>
  4. storpappa
    Member
    Posted 5 years ago #

    Thanks that is what I was looking for. I have seen some very nice themes that for some reason make the poster add custom tags rather then just resize the image. This just makes more sense.

    Yes, I understand they want to add flexibility and the custom fields gives you that option, but like about - just check for fields first and if not - resize

  5. storpappa
    Member
    Posted 5 years ago #

    Notice one thing missing - I accidently loaded a gallery of images into a post, removed the gallery tag but the images were still there - this method pulls all the images in a post. I think for post image thubnails in themes people would probably just need the first image

  6. Michael Fields
    Themer
    Posted 5 years ago #

    Opps...
    Forget that part.
    This should pick the first image sorted by menu order:

    <?php
    $images = get_children(
    array(
    	'post_parent' => $post->ID,
    	'post_status' => 'inherit',
    	'post_type' => 'attachment',
    	'post_mime_type' => 'image',
    	'order' => 'ASC',
    	'orderby' => 'menu_order'
    	)
    );
    
    if ( $images ) {
    $count = 1;
    foreach ( $images as $id => $image ) {
    	if( $count === 1 ) {
    		$img = wp_get_attachment_thumb_url( $image->ID );
    		$link = get_permalink( $post->ID );
    		print "\n\n" . '<div><a href="' . $link . '"><img src="' . $img . '" alt="" /></div>';
    	}
    	$count++;
    }
    }
    ?>
  7. nicholas77
    Member
    Posted 5 years ago #

    mfields, you rock! Thanks a lot!

  8. olddocks
    Member
    Posted 5 years ago #

    since wordpress 2.6.2 thumbnails are handled automatically without need of plugins. you just need to go to settings and set the thumb size.

  9. Klark0
    Member
    Posted 5 years ago #

    Cool, but how do we grab the medium image ?
    I can't find documentation on this anywhere.

  10. umxid
    Member
    Posted 5 years ago #

    exactly what i was looking for, thanks a lot mfields! just one more thing, what if we want to retrieve embeded post video in the same way as pictures thumbnails and also should be good to add alt="" tag for thumbnails may you help with that please?!

  11. actionscripted
    Member
    Posted 5 years ago #

    @mfields

    You have an unclosed anchor tag in your template code. The print line should look like this:

    print "\n\n" . '<div><a href="' . $link . '"><img src="' . $img . '" alt="" /></a></div>';
  12. EraserGirl
    Member
    Posted 5 years ago #

    i am new at this and know just enough php to be dangerous.
    My magazine theme is SUPPOSED to pull a thumbnail from posts using a custom field. but i can't figure out why it DOESN'T work as it is supposed to.
    If it can't find an image is plugs the post title in as alternate text...THAT part works fine.
    I have read the code above, but i have no idea how to implement it.
    Which template do I add this code to?

    MethuenCommon.com
    I have been working on this problem for over a week, and my deadline has sort of passed. can anyone help me? I am obviously inadequate.

  13. EraserGirl
    Member
    Posted 5 years ago #

    hee hee hee...i love it when i ask for help and almost immediately SPY my problem.

    thanks for being here.
    (i hadn't understood the usage of the custom field...Mfields explained it well on his site)

  14. Lars M
    Member
    Posted 5 years ago #

    I'm trying to make an Archives page which lists all the images attached to each page
    (I'm not using posts) and I need each image to link to its respective page.

    Haven't had any luck with the codes I found here on the forum or elsewhere.
    Do anyone know how I would go about doing this?

  15. Lars M
    Member
    Posted 5 years ago #

    Solved my problem, solution here: How to create a page that presents all post images?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags