One of my more recent obsessions when working with WordPress is trying out some different things with media, especially experimenting with attachment pages. For a new theme I’m working on, I wanted to add links to all the image sizes on the image attachment page in a Flickr-esque fashion.
Does WordPress have a function for that?
That’s one of my favorite questions because it allows me to explore WordPress and learn more about the system. WordPress is still lacking in some ways when it comes to handling media, but it does have a great function for my needs in this experiment.
The end result
The goal is to get all the image sizes for an attached image and display a link to each on the image attachment page as shown in the following image.

Creating the functionality
As I mentioned earlier in the post, WordPress has a function for that, which is called wp_get_attachment_image_src(). Judging by its name, you’d think it only returned the URL of the image, but it does so much more. It returns all four values that we’ll need in an array.
- The image source based on the size.
- The image width.
- The image height.
- True/false depending on whether the image is an intermediate size and not the original.
To use this function, we need to create our own function that loops through all the various image sizes for a specific attachment image and returns a list. Drop the following PHP code in your theme’s functions.php file to do this.
function my_get_image_size_links() {
/* If not viewing an image attachment page, return. */
if ( !wp_attachment_is_image( get_the_ID() ) )
return;
/* Set up an empty array for the links. */
$links = array();
/* Get the intermediate image sizes and add the full size to the array. */
$sizes = get_intermediate_image_sizes();
$sizes[] = 'full';
/* Loop through each of the image sizes. */
foreach ( $sizes as $size ) {
/* Get the image source, width, height, and whether it's intermediate. */
$image = wp_get_attachment_image_src( get_the_ID(), $size );
/* Add the link to the array if there's an image and if $is_intermediate (4th array value) is true or full size. */
if ( !empty( $image ) && ( true == $image[3] || 'full' == $size ) )
$links[] = "<a class='image-size-link' href='{$image[0]}'>{$image[1]} × {$image[2]}</a>";
}
/* Join the links in a string and return. */
return join( ' <span class="sep">/</span> ', $links );
}
Showing the image size links on attachment pages
Now that you have a function for displaying links to different image sizes, you need to call the function in the correct theme template. This is trickier because themes don’t always use the same templates. If you’re a theme developer, you should know where to add this. If you’re a user modifying a theme, look for an image.php or attachment.php template. Your theme should probably have one of these files.
Within The Loop, you can add the following code. I chose to add mine after the display of the attachment title.
<?php if ( wp_attachment_is_image( get_the_ID() ) ) { ?>
<div class="image-meta">
<?php printf( __( 'Sizes: %s', 'example-textdomain' ), my_get_image_size_links() ); ?>
</div>
<?php } ?>
That’s all there is to it. Now you can show all those image size links as done on Flickr.
Thanks, that’s incredibly useful for something I’m working on, giving Flickr-style size options hadn’t even occurred to me.
Glad I could be of help. I plan on building my own personal mini-Flickr for my site, so that’s one of the reasons I decided to see how this is done.
(Quote)WordPress is still lacking in some ways when it comes to handling media.
Oh, do you mean like when they output style tags right into the body so the page doesn’t validate? ha!
Great information. I am always trying to find better ways to work with WP’s image attachments. I try not to use gallery plugins and would rather output everything on my own, then incorporate that output into a gallery or presentation system of my choosing.
For example: Recently, I had to split all the image attachments for a post into before and after pics and this little tip could have made it even more slick. Oh well. Next time.
BTW – Does this also find any add_image_size custom sizes or just the sizes set in the media settings?
It should work with any custom image sizes added via
add_image_size(). I haven’t tested it to be sure, but it should.There’s plugin for that.
As soon as I posted I saw the plugin in your sidebar! Thanks!
Wow this is amazing, good job as always.
Cheers,
Emil
Justin, this is a perfect example of what I could use for some client sites that I’ve worked on that have been utilizing featured images. We have worried a bit about what image gets pulled with the possible range of sizes a client may upload. Using the information you showed above, I could make sure we likely pull the necessary one and not break the website layout.
I wonder where you get these crazy ideas, Justin! Awesome as usual.
Well, I got this one from Flickr.
Usually, I just see something cool around the Web and ask, “Can that be done with WordPress?”
Is there any way to put all formats when we insert an image in the post (in lightbox upload)?
For example: http://img15.imageshack.us/img15/2844/helpwr.jpg
Thanks
Hi justin, thanks for great function.., i just want to ask is there any way to make the image link that we click will be open up in image.php page… what i mean something like this :
Size : 150×150 , 300×300..etc
From your function, if i click 150×150.., then it goes to myimage-150×150.jpg..
I just wondering if you could make it appear in image.php if i click 150×150.. not to image location..
the link maybe like this :
Original image.php link
http://www.mysite.com/mytitle/attachment/myimage/
If click on 150×150 goes to here :
http://www.mysite.com/mytitle/attachment/myimage-150×150/
Can it be done..?
Thanks Justin, I will impleament this in my blog…thanks alot for sharing
Is there any working example? I would love to see it, it can be interesting demonstration to show my designers (if I understand correctly what you describe). We launched a new WP site few weeks ago, but handling pictures (since there have been and will be many listings added) has been always challenge.
Hey Justin, yeah you were right. I’ve been using it. You can add the link not Flickr links only but anywhere you wanted your images to be redirected once clicked. This gave more ideas to those who were not doing this yet.
This is the perfect blog for anyone who wants to know about this topic. You know so much its almost hard to argue with you (not that I really would want…hehe). You definitely put a new spin on a subject that’s been written about for years. Great stuff, just great!
I would like to see an example,looks interesting.
Hi Justin
Thanks for the info, I’ve been looking for a fix like this! Shall be implementing it this afternoon once I’ve gotten some coffee in me…. always a nervous time when changing CSS. Shall be doing much backing up first! Fingers crossed
Cheers
Ross
Been trying to do this for a while, thanks for the excellent post, will try it and let you know how it goes.
I should try it, it would be nice to have this in wordpress, going to digg it up more.
OMG Justin thanks you for this post. This is EXACTLY what I was looking for on our new site. Posted to Twitter. THANK YOU!
This is a sweet piece of code. I’m thinking this could be integrated really nicely with some sort of image gallery plugin on a photography style site. I will give this one a try. Thanks Justin!
That is a good code for wordpress. One thing I’m trying to do is making lightbox work only on attachment page, anyone tried that?
Excellent – I was looking all over for this and didn’t find it in the WP documentation. Thanks.
Once again, Justin, you deliver the goods!
I’m developing a site for work, which will be a photo library. Wordpress resizes the images, and users can select a size for print, embedding or using as a thumbnail for our various sites (with various sizes – and yes, custom sizes work).
Next step is to adapt your code to have have them linked by named size-types rather than their dimensions, and to exclude the thumbnail built-in size (because that’s used only by this site). Suspect the latter will be easier than the former – just need to exclude item [0] from the array…
Gary
Justin thanks you for this post. I wonder where you get these crazy ideas!
Congratulations, I was wondering if someone has already done this.
Then I googled “Wordpress all image sizes”… : )
Justin,
Thank you for this post, incredibly helpful.
Thank you Justin, helpful as usual.
Next step, I think it would be better to limit appeared sizes into full-large-middle only. The reason is, I don’t think anybody wants to click small 150×150 thumbnail, so I think it would be better if we can limit the sizes. Can you help to do this?
Oh, this is amazing! As many others here, I really need this function and you made my life a lil’ easier :]
Thanks and have a nice day!
Is there any way to show the size for all images in the post? or even when using the [gallery] shortcode?
If this can be done then please let us know, it will help me a lot.
Hi,
I would also be very interrested to hear how this should be implemented in a lightbox on ordinary pages.
Kind regards
thanks, this really helped!!
Is there a way to display category name and tags list at attachment page?
Although super useful code above, the post actually deals with image dimensions, not the size (as in bytes).
Would you by any chance know of a good way to echo the file size (in bytes) on the single attachment page?
Many thanks in advance.
Piet
Not work for me, i’ve placed the code in the loop but no link show, please can help?
thanks
It only works in The Loop on attachment pages. It doesn’t work on other pages.