WordPress.org

Support

Support » Themes and Templates » Image thumbnails

Image thumbnails

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

Viewing 14 replies - 1 through 14 (of 14 total)
  • @mercime
    Participant

    @mercime

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

    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

    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

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

    mfields, you rock! Thanks a lot!

    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.

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

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

    actionscripted
    Member

    @actionscripted

    @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>';
    EraserGirl
    Member

    @erasergirl

    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.

    EraserGirl
    Member

    @erasergirl

    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)

    Lars M
    Member

    @hrstuks

    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?

    Lars M
    Member

    @hrstuks

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

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Image thumbnails’ is closed to new replies.