hey guys, I couldn't find anything on the web. Is it possible to have custom template for the image attachement page. I wonder if it is possible to add a navigation to the image attachement page so people can easily navigate through a gallery.

Maybe it's even possible to add comments to each image?

edit: Moreover is it also possible to query if I'm currently on an attachement page? E.g. for breadcrumbs I want to insert something like "home > back to post > current_image.jpg"


function breadcrumbs() {
    if (!is_home()) {
        echo "<a href='" . get_bloginfo('home') . "' title='Home'>Home</a> &rang; ";
        if (is_category()) {
            $category = get_the_category(); 
            echo $category[0]->cat_name;
        } else if (is_single()) {
            echo " &rang; ";
        } else if (is_page()) {
            $ancestors = get_post_ancestors($post);
            // echo ancestors
            foreach($ancestors as $id) {
                echo "<a href='" . get_permalink( $id ) . "' title='" . get_the_title( $id ) . "'>" . get_the_title( $id ) . "</a> &rang; ";
        } else if (is_tag()) {
            global $wp_query;
            $tag = get_term( $wp_query->queried_object_id, 'post_tag');
            echo "Tag &rang; " . $tag->name;
        } else if (is_search()) {
            echo " Searchresults &rang; " . get_search_query();
        } else if (is_404()) {
            echo "Not found";
        //} else if ( is_attachment() ) {
        } else if ($post->post_type == 'attachment') {
            //echo "<a href='" . get_permalink() . "'>" . get_the_title() . "</a>";
            echo "doesn't work?";

2 Answers 2


WordPress supports several types of attachment templates. The function get_attachment_template in wp-includes/theme.php provides this support; it is called in wp-includes/template-redirect.php. If your theme includes attachment.php all of your attachments will be rendered with that template. If your theme also includes image.php then all of your images will use that template as long as they have a post_mime_type of image/*.

It is certainly possible to add gallery navigation. See wp-content/themes/twentyten/loop-attachment.php which uses functions previous_image_link and next_image_link.

Twentyten's attachment template does call comments_template so you can collect comments on each photo. You just have to make sure your post links to the attachment pages and not directly to the images.

You can call is_attachment to determine whether the queried object is an attachment.

  • thank you, I updated my post. I'm using the twenty ten templates now an will try to adapt them to my needs. Anyway, only thing I can't find out is how to make my breadcrumbs work so they show home > current post title as link > current image Commented Apr 5, 2011 at 10:55

Short answer - Yes.

Attachments are served by template files based on their mime-type, So if your attachment is an image/jpg, for example, WordPress will first look for a template file named image.php, then jpg.php, and then finally attachment.php, before falling back on another template file (single.php or index.php). There's a good chart showing the template hierarchy on the Codex page.

This only works if you're accessing the attachment through WP's rewrite structure (ie. when you insert the image, choosing the "link to post" rather than "link to file" option. The url should look something like domain.com/post-name/attachment-name ... Accessing the file URL directly bypasses WordPress temple system.

And, if you want to check if you're on an attachment page, you can always check the post type, ie.

if ($post->post_type == 'attachment') {

Of course, within an attachment.php template, there's no readon to bother checking that - you already know you're on an attachment page.

  • Creating gallery navigation links (using thumbnails) isn't difficult. Take a look at attachment-loop.php in TwentyTen. Also, any reason not to use is_attachment(), rather than $post->post_type == 'attachment'? Commented Apr 4, 2011 at 19:59
  • @Chip and @Andy - No, no reason at all. I've never had to use the is_attachment() conditional, but you're right, thats a better approach. It should be available from much earlier, whereas $post->post_type is only available in the Loop. Commented Apr 4, 2011 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.