Beginner's Guide for WordPress / Start your WordPress Blog in minutes

How to Create a Custom Single Attachments Template in WordPress

In the process of creating a total gallery display in WordPress powered by Custom Post Types, we have shared the code for how to get all post attachments in WordPress except for Featured Image. We have also showed you how to add additional fields to the WordPress Media uploader. In this article, we will show you how to use the additional fields in the media uploader and display them in a custom single attachments template.

Before we start, it is important that you understand how the template hierarchy works for attachments. You can either specify it like mime_type.php. Mime type examples (image.php, video.php, application.php etc). For specifying it just for a certain image type, you can say image_gif.php. If the mime_type.php is not found, then it looks for attachment.php, then single-attachment.php, then single.php, then index.php.

It depends on how custom of a project you are working for, you may not need to create image_gif.php. You can simply suffice for image.php or even attachment.php.

All you have to do is open a blank file and copy all of your single.php content in there. You can save it as single-attachment.php or any other mime_type that you want. Then replace the loop code with something like this:

if ( have_posts() ) : while ( have_posts() ) : the_post();
$photographer = get_post_meta($post->ID, 'be_photographer_name', true);
$photographerurl = get_post_meta($post->ID, 'be_photographer_url', true);

<h1><?php the_title(); ?></h1>

<div class="photometa"><span class="photographername"><?php echo $photographer; ?></span> // <a href="<?php echo $photographerurl ?>" target="_blank" class="photographerurl"><?php echo $photographerurl ?></a></div>

                        <div class="entry-attachment">
<?php if ( wp_attachment_is_image( $post->id ) ) : $att_image = wp_get_attachment_image_src( $post->id, "full"); ?>
                        <p class="attachment"><a href="<?php echo wp_get_attachment_url($post->id); ?>" title="<?php the_title(); ?>" rel="attachment"><img src="<?php echo $att_image[0];?>" width="<?php echo $att_image[1];?>" height="<?php echo $att_image[2];?>"  class="attachment-medium" alt="<?php $post->post_excerpt; ?>" /></a>
<?php else : ?>
                        <a href="<?php echo wp_get_attachment_url($post->ID) ?>" title="<?php echo wp_specialchars( get_the_title($post->ID), 1 ) ?>" rel="attachment"><?php echo basename($post->guid) ?></a>
<?php endif; ?>

<?php endwhile; ?>

<?php endif; ?>

The code above simply displays the Image Title. Below it, it will display Author’s name and URL which we added as additional fields in our previous article. Then it looks for the image and displays the full size. You can customize it to show any other size if you have additional image sizes.

Final Outcome:

Single Attachment Example

Now that we have covered all three main aspects of this gallery that we were going to create, in the next article, we will show you how it looks when all things are pieced together. This way, you can use WordPress’s built-in mechanism to create a full-featured gallery with albums. No extra plugins required.

Template Hierarchy Codex

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit – a collection of WordPress related products and resources that every professional should have!

Reader Interactions

8 CommentsLeave a Reply

  1. How to open any image from article in a new page like this:

    And how to show all images from article at the bottom of this image page (plus NEXT, PREV button)?

  2. I created a attachment.php but it seems not to work. The attachments pages and its layout still look like before. Have I missed something?

  3. Thanks for this post. It really helped me.

    I had a problem where Google indexed one of my attachment pages as if it were content. I looked up recipes for fixing it and nothing seemed to work. Yost’s SEO plugin does this, but I am using another and I don’t want to change it. Other suggestions worked, but they affected some of my custom post type work.

    Then I saw this post and realized that all I had to do is create a single-attachment.php page with javascript that refreshes to the homepage and it works great!

    Does anyone know if there is a down side to doing this?

  4. Monolops, you can attach .zip files to a post and when adding them to a post you can choose it to link to the attachment page instead of file. Now when users will click on the file link they will be taken to attachment page.

    Check out how the Twenty Thirteen theme displays .zip files differently than images by studying its attachment.php template.

    An easier solution could be using WP File Manager plugin.

    • Hey thanks for point that up. I will look for attachment page in that theme..I know that I can point .zip file to attachment page but don’t know how to customize it..That’s my issue.. Wp file manager is nice plugin but only offers direct donwloads. But with attachment page you get one more nice spot for ads when someone want to download those free files..

  5. Very nice tutorial..I am wordpress beginer in customizing and I love your website. Can you please help me with this matter but instead of photography I need file attachment like .zip or .rar. I’ve tried to work on my own based on your tutorial but my skills isn’t on that level yet :) Every google search for customizing attachment page is about images. No one thinks that people might want to share files i guess…

  6. I am interested to use single-attachment.php to display the attachments with some content.
    However, what I’d really like is to get the list of such pages into my sitemap, and I could not achieve that with existing plugins (one list URL of images,but not URL of image/attachment pages)
    So I am thinking to have a page or achive that would list all URLs of image/attachment pages. How would I do that?

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.