new gallery feature in WordPress 2.5
March 30, 2008When I saw the new image and media management built right into the new WordPress 2.5, I was very excited. It seems a lot easier, at least on the face of it. One problem, however, is that there’s precious little documentation on how to use the new features. For example, it took me forever to figure out how to get the nice thumbnail prev-next navigation that Matt shows on his blog. So here’s a quick tip for theme developers trying to use the new gallery feature:
You’ve got a new template you can use: image.php (yee ha!) The new gallery feature seems to set up ’sub-posts’ for each of the images attached to a post or page. If there is no image.php in your theme, it will use the single.php template (best as I can tell, anyway) to display them. But you can create this image.php template and use new template tags to display the thumbnail navigation if you wish. (You can also change the layout or whatever else you’d like as well, of course.)
The new thumbnail navigation template tags:
previous_image_link() and next_image_link()
Include these in your image.php template file the way you would normally include template tags. Check the Default theme that’s bundled with the new 2.5 install to see how it’s done there. You also have to deal with new template tags like:
wp_get_attachment_image() and wp_get_attachment_url()
Again, the only existing documentation seems to be the image.php file itself in the new version of the Default theme.




How do Shortcodes come into this (if at all)?
The gallery thing has a shortcode, and that’s how you put it into a post or page. So in practice, [gallery] is just a call to a function that grabs all the image attachments to a post and displays them. So the [gallery] shortcode is a use of the Shortcode API.
This post has more to do with theme development and the new tags available to display image attachments. This idea of attachments to posts is new to WP. It allows for cool things like this new gallery display, but it requires a new template if you want to fully take advantage of it.
The Shortcode API is mostly of interest to plugin developers, I think. It’s a quick and easy way to have plugin users inject your plugin output into a post or page.
[...] bit of sleuthing revealed the solutions. I found out that the individual image gallery pages look for an image.php template. Also, these pages use the [...]
Is there some special magic needed to get wp_get_attachment_image() to work? Because I cannot get it to fire to save my life!
If you don’t have image.php, the gallery falls back to single.php. Without single.php, the gallery uses index.php. The function tags are sane.
I’m ambivalent about the gallery, though. The controls for it are fantastic, but the presentation is not so much. I need the time to write a plugin to make what I want of it; template hacking only goes so far.
Sean - I’m not sure what to tell you except to copy from the image.php that comes with the Default theme. You might be able to cull some more information from this thread on the forums, too: http://wordpress.org/support/topic/164216
Art - it feels not ready for prime time to me. I like the ease of uploading images, but if a newbie can’t use the gallery part of it out of the box, then it seems a bit unfair to flaunt it as one of the big new features.
Thank you for this - you saved my sanity.
hi, this is actually very helpful, thanks a lot :)
[...] deeper search revealed that the gallery feature relies not only on the new uploader, but also on a new template [...]
Thanks so much for this post! I was able to copy the new image.php and hack away until it suited me fine — only took about 20 minutes to incorporate into the theme I’ve adapted, with a few changes (and also I’ve added a link back to the original post under the nav images).
It’s only a very new site and has lots to be developed (only started work on this last night), but it’s at africanimpact.com.au if you see the blog item for Cape Town under “project updates”, you can see how I’ve administered it.
Still 1 thing I am having trouble changing which I hope they fix: on the post sub-page, I cannot turn off comments (without hacking the image.php like I have to remove the comment facility altogether) — it should refer to the comment setting for the actual (parent) post, but it doesn’t seem to.
Anyway, your article helped me getting it working, albeit with a fair bit of work to the default image.php. Thanks so much!
[...] a post outlining some of the basic functions of the new template option over at Laura Fisher’s blog, and there’s a little bit of information about the gallery shortcodes in the WordPress [...]
[...] and next image and link as you navigate through the photos in your gallery post. Hat tip to mitten. I should apply this to my first and only (so far) gallery post I created a month ago. [...]