This fun little WordPress plug-in pulls in attached images from either a page or post. It’s a handy little function I’ve been using for a long time and adding new features to on a project per project basis, so I figured, why not release it out into the wild? I hope some of you find it useful. Below is a quick run down of how it works, which is quite simple:
<?php echo get_attached_images(); ?>
Like most functions, it can take a few arguments. Below are the defaults and I will give a quick note about each.
<?php echo get_attached_images($pageid = FALSE, $size = "thumbnail", $rel="internal", $single = FALSE, $prepend = "", $append = ""); ?>
To avoid confusion, here is how it would look placed in your theme’s template file(s):
<?php echo get_attached_images(FALSE, "thumbnail", "external", FALSE, "", ""); ?>
Boolean. By default, this pulls in the current page ID, but you can also pass in an ID manually. This comes in handy if you want to display images in your sidebar, say like sponsor images or some such. By default it is set to FALSE.
This accepts the same arguments as the wp_get_attachment_image function, namely: thumbnail, medium, large and full.
This parameter accepts either “internal” or “external” as an argument and it adds the rel attribute to your link, if one exists.
Boolean. TRUE pops the first image off the array. FALSE pulls all attached images. By default it is set to FALSE.
Accepts any HTML tag you want to precede the image. This is useful if you need to wrap the image in a <li> marker, such as if you’re using something like jCarousel.
Accepts the closing tag to match the tag passed in via $prepend.
There are two other features that are also enabled that you might not notice unless you check out the source code. 1) You can wrap your image in a link but it might not be how you expect. When uploading your image(s), under the description field you can add a full link, such as http://google.com. This will link your image. 2) You can also add a caption by filling out the caption field on the upload form. This will place <span class="get_attached_caption">Your Caption</span> after your image but before your appended HTML tag, should one exist.
That’s pretty much it; easy! If you have any thoughts, comments or suggestions about this plug-in, I’d love to hear them so email me at patrick@solidhex.com. I’m sure it could be improved and more features could be added so let those suggestions fly!
http://solidhex.com/code/get_attached_images.zip
Or, get it from the WordPress codex:
http://wordpress.org/extend/plugins/get-attached-images/