An Introduction To WordPress Attachment Pages

Attachment pages are not very well known or understood in WordPress. Each item you upload through WordPress – be it image, audio, video or anything else – can have its own page with content, just like any other post. By making sure your attachment pages are well-formed, and contain relevant information, you can add some depth to your website and even increase your SEO score in the process.

In addition, attachment pages allow users to comment on images which may be great for media-related professions. In this article I’ll tell you a little bit about this relatively obscure page type and how you can add it to your theme using a bit of code.

Understanding Attachments

If you use WordPress but you aren’t into development it may surprise you that uploaded images are stored exactly the same way as posts – in fact, they are posts, just like pages.

What differentiates pages, posts, images and custom post types is the post_type attribute they have in the database. Posts have a post type of ‘post’ (hence the naming confusion), pages have a post type of ‘page’ and attachments have a post type ‘attachment’.

The reason that attachments are stored as posts is quite obvious, once you think about it. An uploaded image has a title, an author, a publication date and so on – data used by regular posts as well. In fact, the only additional bit of information is just the image location. This is conveniently stored in the post meta table.

As a result, an attachment page is nothing more than a single page for an attachment. Just like a post page shows a single post, a static page shows a single page; an attachment page shows a single attachment.

An Example Attachment Page

The upcoming beautiful Twenty Fifteen theme has a great example of an attachment page.

When I uploaded this image I filled out the title, the caption, the description and the alt text. These are all used on this page very well. Note the inclusion of the image size in the post footer, a great addition!

Does My Theme Have An Attachment Page?

The answer to this question is yes and no. First of all, let’s take a look at how you can get to an attachment page.

In any old post, add an image and make sure to select “Attachment Page” in the link to dropdown. Update or publish your post and visit it in the front end. By clicking the image in the post you’ll be taken to an attachment page. It may not look good (depending on your theme) but it is an attachment page.

To understand why this is so, let’s learn a bit about template files. WordPress uses the template hierarchy to determine which file is used when a specific page is visited. Your front page probably uses index.php if it shows a list of posts; pages use page.php; single posts use single.php and so on.

It is called a hierarchy because there is a set hierarchical order in which the correct file is chosen. For example, let’s say you visit the tag archive for your “music” tag. WordPress looks for a tag-music.php file. If it doesn’t exist it looks for tag.php. If that doesn’t exist it uses archive.php. Eventually it falls back to index.php which is a required file so it always exists.

What this boils down to is that index.php is used whenever files further up in the template aren’t defined. Theme authors are used to single pages, archives, 404 pages and such but many forget about our friend the archive page. They forget to implement it and thus index.php is used.

if this is unintentional and the index.php file is not built to handle images, well, you may get some weird-looking attachment pages. If the theme author has implemented attachment.php or has built index.php that is mindful of attachments, your attachment page will look fine.

How Do I Create An Attachment Page?

If your theme doesn’t already have an attachment.php file you’ll need to create it.

If it is a theme created just for you, go ahead and add the file now. If however, you are using a third party theme you downloaded or installed from the repository, you should create a child theme. Child themes are used to extend existing themes without editing the original files. Once you’ve added a child theme (it’s super simple), create an attachment.php file in it.

Find the single.php file in your theme, copy the whole content and paste into the attachment page file you just created. In 70% of the themes out there this should be enough.

In a large portion of the remainder, you may want to delete some single post specific things. In some other cases, due to the way your single post page works, this may still not work as expected. If this so, try using the content of page.php instead.

The Benefits Of Attachment Pages

First of all, attachment pages exist on your site whether users visit them or not. This means that they could potentially be discovered by search engines. Why not make them both search engine and user friendly, just in case? By choosing relevant images for your content, writing nice captions and image descriptions you can add that much more relevant content to your site, increasing your SEO standing while providing more for your users to do.

Since attachments are posts they also have their own comments. If you’d like your users to interact with your images, attachment pages are a great place to do this. This is especially relevant for photographers, artists or musicians.

As a WordPress developer for me the attachment page is also a way of judging whether a theme developer takes his/her theme seriously. If the attachment page shows the same care and attention to detail as other pages it is an good indicator of a decent theme.