X

Linking to All Image Sizes Flickr Style

Snippets by IsItWP

Do you want to automatically link to all image sizes? While there’s probably a plugin for this, we have created a quick code snippet that you can use to linking to all image sizes Flickr style in WordPress.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

function my_get_image_size_links() {
	if ( !wp_attachment_is_image( get_the_ID() ) )
		return;
	$links = array();
	$sizes = get_intermediate_image_sizes();
	$sizes[] = 'full';

	foreach ( $sizes as $size ) {
		$image = wp_get_attachment_image_src( get_the_ID(), $size );

		if ( !empty( $image ) && ( true == $image[3] || 'full' == $size ) )
			$links[] = "<a class='image-size-link' href='{$image[0]}'>{$image[1]} &times; {$image[2]}</a>";
	}
	return join( ' <span class="sep">/</span> ', $links );
}

Add this code snippet to the image.php or attachment.php template file of your WordPress theme.

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

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 50+ best WooCommerce themes and how to create a WordPress donation form.

Comments  Leave a Reply

  1. not work aniway

  2. Thanks for this snippet – works great! I’m trying to display only certain images sizes but had no success so far. Any hint how to do that?

    Also I’d like to show these links on the single post template…
    Cheers,
    Thomas

    1. Hi Thomas, well $sizes is an array you could add a statement within foreach to only display specific sizes.

      1. Ok, I see, thanks! And regarding the display on the single post template?

        Cheers,
        Thomas

        1. you can create a page template see link for details. Then you can add above code to template file. http://codex.wordpress.org/Pages

  3. This is awesome!! love this..!

    1. Cool glad to hear it. 

  4. S.K. (Insfired) May 25, 2011 at 2:43 pm

    Kevin, I was looking for something like this a few weeks ago and gave up after not finding a simple solution for a code-dummy like me. Lucky to have stumbled across your blog though, gonna go try it! =)

    1. cool glad I could help out! don’t forget to follow us I post new snippets daily!

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!
[60% OFF Today!]
[60% OFF Today!]
[10% OFF Today!]
[10% OFF Today!]
[50% OFF Today!]
[50% OFF Today!]