Ferodynamics Network

popular: profile privacy, mobile privacy

January 8, 2009

One of the best ways to liven up a blog, and something that most of the premium themes look to do by default, is adding images to your posts. The usual method of doing this is by adding the image URL via a custom field. But there is a better and more flexible way.

When you upload images in your post they are all added to the database. This makes it very easy to get a list of all the images attached to the post. In addition, WordPress has a built in function to retrieve the appropriate image size as well, meaning you can use a thumbnail on archive pages and a medium image on post pages without messing around with resizing images and adding multiple custom fields.

Below is a short function that I put together that does everything you need. Pop this in the functions.php file you will have an extra template tag: the_image.

[php]
function the_image($size = 'medium' , $class = ''){
global $post;

//setup the attachment array
$att_array = array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order_by' => 'menu_order'
);

//get the post attachments
$attachments = get_children($att_array);

//make sure there are attachments
if (is_array($attachments)){
//loop through them
foreach($attachments as $att){
//find the one we want based on its characteristics
if ( $att->menu_order == 0){
$image_src_array = wp_get_attachment_image_src($att->ID, $size);

//get url - 1 and 2 are the x and y dimensions
$url = $image_src_array[0];
$caption = $att->post_excerpt;
$image_html = '%s';

//combine the data
$html = sprintf($image_html,$url,$caption,$class);

//echo the result
echo $html;
}
}
}

}

What this function does, in a nutshell, is to get a list of all the images attached to a post. It then finds the first image with a sort order of 0 and echoes the html to display it. By default all images have a sort order of zero unless you choose to reorder them. If you don’t make any changes then it will find the first image. If you reorder them using drag and drop the first image in the list will have an order of 1 and you would need to change it to 0.

To change the sort order of an image you need use the gallery tab:

Sort order panel

Using the tag

Using the tag is very simple. Insert the tag into the loop just above the the_content tag and select what size image you want, and what css class to attach to the image. I have used:

[php]
//in the single post page

//in the index.php page

The result looks something like this (with a little CSS).

Index

Home page with thumbnail

Single

Single post page with medium image

Updated version

An updated version using many of the suggestions made in the comments is available in the easy peasy image suggestion roundup post.

Wordpress Chat
one comment
page 1308
Html 5 Gallery
6 comments
page 1305
Html 5 Gallery
6 comments
page 1305
Silence Is Golden
3 comments
page 213
Questions About Habari For Wordpress Users
6 comments
page 424
Theming Habari Vs Wordpress
13 comments
page 440
My Experience Of Flexx
4 comments
page 1026
Plugin Update Fun With Photo Data 2
one comment
page 815
Post Image The Easy Peasy Way
26 comments
page 1065
Gaining Benefits From Plugins
8 comments
page 1167
Categories Vs Tags Either Neither Or Both
12 comments
page 7
Fun With Theme Widgets
24 comments
page 867
Beware Wp Cache
8 comments
page 1310
Six Million Ways To Die Choose One
14 comments
page 1128
Post Image The Easy Peasy Way
26 comments
page 1065
Post Image The Easy Peasy Way
26 comments
page 1065
Wordpress Chat
one comment
page 1308
Post Image The Easy Peasy Way
26 comments
page 1065
Beware Wp Cache
8 comments
page 1310
Wp Polls Reviewed
one comment
page 58
Wordpress Chat
one comment
page 1308
Fun With Photo Data
12 comments
page 330
Fun With Sidebar Tabs Styling
2 comments
page 336
Html 5 Gallery
6 comments
page 1305
Using Your Own Url Shortener
4 comments
page 1190
Html 5 Gallery
6 comments
page 1305
My Experience Of Flexx
4 comments
page 1026
Fun With Sidebar Tabs
193 comments
page 57
Html 5 Gallery
6 comments
page 1305
Fun With Plugins
27 comments
page 14
Wordpress 25 Exif Fields
12 comments
page 230
Html 5 Gallery
6 comments
page 1305
Html 5 Gallery
6 comments
page 1305
Html 5 Gallery
6 comments
page 1305
Quick N Dirty Replacement Text
no comment
page 122
Beware Wp Cache
8 comments
page 1310
Theming Habari Vs Wordpress
13 comments
page 440
Fun With Sidebar Tabs Styling
2 comments
page 336
Beware Wp Cache
8 comments
page 1310
  updated 1 seconds ago
Monday, 9pm
Andrew Rickmann

That isn't part of the function but it is a very simply matter to add it in.

Monday, 8pm
Josh Byers

Great tutorial! This helps me so much!

Is it possible to call individual images attached to the post based on their sort order using this function?

The code might look like this:
<?php the_image('medium','post-image'); ?> Default, calls the image with the sort order of 0
<?php the_image('medium','post-image','1'); ?> Calls the image with the sort order of 1
<?php the_image('medium','post-image','2'); ?> Calls the image with the sort order of 2

And so on…

Saturday, 7pm
tonygray216

Wow! That is great! You are da man…thanks for the quick turn around.

Saturday, 6pm
Andrew Rickmann

Take a look at my next post Vivien. I have updated the function to do what you want:
http://www.wp-fun.co.uk/2009/01/10/easy-peasy-i...

Saturday, 6pm
Andrew Rickmann

Tony, I have updated the code in my new post: http://www.wp-fun.co.uk/2009/01/10/easy-peasy-i...

I don't know much about thickbox so I have implemented the default, just as you have, and got the same results.

Saturday, 6pm
inspirationbit

Thanks, Andrew. So basically, you're saying that I should leave the things as is on my blog: use a custom field to display the featured image and make this image available in RSS with the hook?

Saturday, 4pm
Tony Gray

great! I'm looking forward to it. Maybe you will help me figure out why I'm seeing the close image instead of the text. You can see what i've done here. http://blog.tgrayimages.com/

Saturday, 9am
Andrew Rickmann

Hi Tony,

There is another way to do it which would be more flexible. Look out for the follow up post where I roll up the suggestions and I will include it there.

Saturday, 6am
Tony Gray

This will be one of the most useful tutorials in my toolbox. I can't tell you have much headache you have just saved me. I've been trying to teach my clients how to post images using the custom fields it and freaks them out. This is AWESOME. I made a couple of additions to your function. I wanted to be able to click on the image and show the large image in the jquery thickbox. Here is the modified code and all seems to be working great! I enqueued jquery and thickobx and then modified the anchor tag. Let me know if there is a better way.

Thanks for the code!


function tb_enqueue() {
wp_enqueue_script('jquery');
wp_enqueue_script('thickbox');
}
add_action('wp_head', 'tb_enqueue', 1);

function the_image($size = 'medium' , $class = ''){
global $post;

//setup the attachment array
$att_array = array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order_by' => 'menu_order'
);

//get the post attachments
$attachments = get_children($att_array);

//make sure there are attachments
if (is_array($attachments)){
//loop through them
foreach($attachments as $att){
//find the one we want based on its characteristics
if ( $att->menu_order == 0){
$image_src_array = wp_get_attachment_image_src($att->ID, $size);
$image_src_array_large = wp_get_attachment_image_src($att->ID, "large");

//get url - 1 and 2 are the x and y dimensions
$url = $image_src_array[0];
$url_large = $image_src_array_large[0];
$caption = $att->post_excerpt;
$image_html = '<img src="%s" alt="%s" class="%s" />';

//combine the data
$html = sprintf($image_html,$url_large,$url,$caption,$class);

//echo the result
echo $html;
}
}
}
}

Friday, 7pm
Ben Whitehouse

Thanks Andrew. I did a little dance and hacked something together that now works. Thanks!

Friday, 6pm
Andrew Rickmann

Ben, the sprintf function replaces the instances of %s in the first of the arguments with the text in subsequent arguments, so sprintf('','Link','Title') would replace the first %s with link and the second with title.

Doing what you want would be easy. You need to wrap the sprintf statement in an if…else block that checks if you have included 1 or not. If it has then you use a version with the link html in it, otherwise you use the version without (the one that is there already).

I think the permalink is in $post->permalink.

Friday, 6pm
Ben Whitehouse

Okay, I tried to edit the fuction to do this, but I'm not entirely sure how you got the “%s” to do your bidding, so I'm going to ask, very nicely, for a little guidance. Is there a way I could roll in a link to the permalink using a third attribute in the function? like:

<?php the_image('medium','post-image','1'); ?>

where '1' is to add the permalink and '0″ is to not include it? My problem is I want to have the image link to the article, but thusfar I have to add it manually, regardless of if there is an image or not. Maybe I should just learn how to code :( I'm trying to figure it out on my own, but any advice would be greatly appreciated. thanks.

I am a baby. Great tutorial!

Friday, 6pm
Ben Whitehouse

Yes, Wordpress has by default 3 sizing options (thumbnail, medium, large) which can all be set in under the “Setting > Media” tab in your wordpress install.by selecting any of these options the tag will return the appropriate sized options. It will not scale an existing image to fit.

Friday, 5pm
ioni

Can be easily refined to de many things, but this is a very good one!
Certainly gonna use it!

Friday, 5pm
Ben Whitehouse

YOU ARE BRILLIANT! I was looking to use multiple image sizes on a web site I just launched last week (365inches.com) and let me tell you I looked and looked. In the end I came up with a REALLY convoluted solution. I need to recode my image calls pronto!