Oneupweb : Using Attached Images in WordPress for Galleries

Posted on in Blog

Thumbnail SettingEver wanted to be able to create custom image galleries unique to posts and pages on your WordPress site? WordPress’ Media Library allows you to do just that without the need for plugins. I’ll walk you through the code necessary to get it done.

First of all, you need to set up the dimensions of your thumbnails by going into the WP Admin and clicking the “Media” link under settings.

I’ve opted to go with a 125×125 pixel dimension for this demonstration. Now, whenever I upload an image, WordPress will automatically scale down and crop a square thumbnail of my image.

Once you upload an image, you will need to “attach” it to the post or the page’s gallery you want it to be a part of by clicking the “Attach” link and locating your page or post.Attaching Image

The next thing you’re going to have to do is open the functions.php document and insert the following code…

//Gets page/post's attached images for a gallery function
function get_attached_gallery() {
if($images = get_children(
'post_parent' => $post->id,
'post_type' => 'attachment',
'numberposts' => -1, // You may set this to any number you wish or -1 for all images
'post_mime_type' => 'image',
//Uncomment the line below if you use "post thumbnails" in your template already and wish to exclude them from the gallery
//'exclude' => get_post_thumbnail_id($post->id)
echo '

<ul class="attached_gallery">';
foreach($images as $image){
$thumbnail = wp_get_attachment_image($image->ID,thumbnail);
$imagelink = wp_get_attachment_url($image->;ID);
echo '

	<li><a href="'.$imagelink.'">'.$thumbnail.'</a></li>

}echo '</ul>


Now, all you have to do is call the function get_attached_gallery() within the loop wherever you’d like WordPress to generate your gallery. It will return an unordered list with each image inside a list item, and link to the original, full sized upload. Style the gallery however you wish with CSS, or use a plugin like jCarousel to turn your list into a scrollable filmstrip.

Up Next

Instagram feed posts are a nifty way to pair stunning visuals with neat and concise copy. But what if what you’ve got to say is more than 2,200 characters? Brands still haven’t picked up on Instagram Guides, a feature on IG business and creator accounts that allows for long-form text that supports products, services and...

Read More