pda-wordpress-attachment-page

WordPress Attachment Page: An Ultimate Guide

If you are dealing with WordPress, you have probably heard the term “WordPress attachment page” at least once.

Have you understood it well?

This article will shed light on the WordPress attachment page and how to make full use of it.

But first, let’s find out what an attachment is.

Understanding WordPress Attachment

An attachment refers to a type of uploaded item in WordPress. It can be a picture, a zip file, a video, or a PDF document.

You can add attachments to WordPress via the post edit screen. When you submit files through the Add Media button, they automatically become attachments to that post. WordPress saves these attachments in the post database, where standard posts and pages are kept.

Files that you upload directly to the Media Library are not associated with a certain post or page. However, each file upload has its own permanent links. Once you insert a file link to a post, it will become an attachment.

What Is a WordPress Attachment Page?

Attachment pages are the same as “Posts” and “Pages”, but they have a separate post type called “Attachments”. Check out our article to understand more about different WordPress post types.

Simply put, a post page shows the content of a single post. A static page shows the content of a single page. An attachment page shows the content of a single attachment.

Just like a post or page, an attachment page has its own URL. When you enter the attachment page URL, you will see something like below:

pda-wordpress-attachment-page-example

Attachment pages let you add metadata to the files you upload, including title, caption, and description. They also enable visitors to leave feedback on your photographs and videos. Of course, it doesn’t stop there.

If you’re curious about other benefits of WordPress attachment pages and how to use them, keep reading the article.

WordPress Attachment Page Template

When users access a page, WordPress uses the template hierarchy to identify which template is used to display the page. The chosen one is determined by the templates available in your theme as well as the WordPress template hierarchy.

pda-wordpress-attachment-page-template

To illustrate, your front page probably uses index.php if it shows a list of posts. The image.php file controls how image attachment pages look. Video attachment pages are under control of the video.php template. You can also create custom templates for different purposes.

Some themes support the attachment.php template to control the appearance of your attachment pages. However, in case your WordPress theme doesn’t offer a specific template for attachments, it uses single.php by default. This is the single post type template.

In this case, your attachment page won’t look good as it has the layout of any other post or page. Thus, if your theme doesn’t have an attachment.php file as your demand, just create one.

How to Create a Custom Attachment Page Template

#1 Using Child Theme

In case you’re using a theme created just for you, add the file right now. If you download a ready-made theme, you must create a child theme to generate an attachment.php file.

Child themes help you extend the existing parent theme without messing with the original files. After adding a child theme, generate an attachment.php file there.

  1. In your child theme, add an attachment.php file.
  2. Return to the parent theme and copy all of the single.php file’s content and paste it into the new attachment.php file. The attachment page should be back to normal when you visit it again as it’s using the same code. This ought to be adequate for 70% of the available themes.

You can eliminate parts of the remaining content that is exclusive to specific posts. This sometimes still does not work as expected due to the way your single post functions. If this is the case, consider utilizing page.php‘s content instead.

#2 Adding Code

You can make your own unique page for all attachments or just for image attachments. The attachment.php file contains code that is similar to the single.php post page. Again, you should make your child theme active and ready, then follow the steps:

  1. Create a new file using a code editor like Notepad++, TextEdit, Sublime Text, or any text editors.
  2. Add the following code to the new file:
1 <?PHP
2 /**
3 * This file creates a custom attachment page for any Genesis child theme.
4 * @author    Annie
5 * @example   https://preventdirectaccess.com/web-design/genesis-basic-custom-attachment-page/
6 */
7
8
9 //* Add landing body class to the head
10 add_filter( 'body_class', 'wpsites_attachment_class' );
11 function wpsites_attachment_class( $classes ) {
12
13 $classes[] = 'wpsites-attachment';
14 return $classes;
15
16 }
17
18 //* Force full width content layout
19 add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
20
21 //* Remove site header elements
22 remove_action( 'genesis_header', 'genesis_header_markup_open', 5 );
23 remove_action( 'genesis_header', 'genesis_do_header' );
24 remove_action( 'genesis_header', 'genesis_header_markup_close', 15 );
25
26 //* Remove post info from entry header
27 remove_action( 'genesis_entry_header', 'genesis_post_info', 5 );
28
29 //* Remove breadcrumbs
30 remove_action( 'genesis_before_loop', 'genesis_do_breadcrumbs' );
31
32 //* Remove the entry meta in the entry footer
33 remove_action( 'genesis_entry_footer', 'genesis_post_meta' );
34
35 //* Remove site footer widgets
36 remove_action( 'genesis_before_footer', 'genesis_footer_widget_areas' );
37
38 //* Remove site footer elements
39 remove_action( 'genesis_footer', 'genesis_footer_markup_open', 5 );
40 remove_action( 'genesis_footer', 'genesis_do_footer' );
41 remove_action( 'genesis_footer', 'genesis_footer_markup_close', 15 );
42
43 //* Run the Genesis loop
44 genesis();

3. Name the file attachment.php or image.php.

4. Upload the file to the root directory of your child theme folder.

Now that you have created your attachment page. Let’s move to how you view your attachment page in WordPress.

How to View an Attachment Page in WordPress

Viewing an attachment page can let you see the changes after customizing your single attachment template. Here’re a couple of ways to view it in WordPress.

#1 From the Media Library

To access an image or other attachments from the Media Library:

  1. Go to Media > Library and click on the file. The Attachment details section will appear.
  2. You can enter the image’s title, caption, and alt text here. It’s a good idea to fill up these fields for better SEO.
  3. Click View attachments page and you will be directed to the attachment page of that image.

pda-view-wordpress-attachment-page

#2 From a Post or Page

To view the attachment page from a post or page:

  1. Open a post or page in the WordPress content editor and then add the image you want.
  2. Click the image, select the Link button, and choose the Attachment Page option from the dropdown menu.

pda-view-wordpress-attachment-page-from-post

The Benefits of Attachment Pages

As we mentioned, attachment pages allow you to add metadata to your file uploads. It means that you can add a title, caption, and description to your files. Also, visitors can leave comments on your images and videos.

Besides, your site has additional pages whether users can visit them or not. They can potentially discover these pages on search engines. Hence, it’s necessary to include relevant images in your content with beautiful titles and visual descriptions. In this way, you can improve your content quality and increase SEO.

If you want your users to interact with your images, attachment pages come as a great way to do so. However, attachment pages sometimes affect your site SEO and pull down the page’s rankings without proper use. Learn more about the reasons why attachment pages are bad here.

Disabling Attachment Pages in WordPress

We’ve discussed how an attachment page can help with SEO and make your site easier to use. But in most cases, attachment pages do more harm than good. That’s why you should consider disabling attachment pages.

Many themes don’t have specific layouts for attachments, which can make them appear inadequate on your website. Visitors might get a negative impression as a result.

There will be some side effects on your SEO if you abuse attachment pages, too. This is because search engines view pages with little to no text as having “thin content” or “poor quality”.

So, how can you disable it?

There are 2 main ways to achieve that. The first method makes use of WordPress plugins. In the second one, you can add custom code to WordPress. The former seems easy to carry out since it requires no coding skill. But you should choose the method that suits you best.

#1 Use a WordPress Plugin

To disable the attachment page, we recommend the All in One SEO plugin. Once you activate the plugin, there will be a new All in One SEO menu in your admin dashboard.

  1. Go to All in One SEO > Search Appearance and then click the Image SEO tab.

pda-all-in-one-seo-image-seo

2. Three options will appear in the Redirect Attachment URLs section. Here you can disable the attachment page completely by choosing the Disabled option. Or, decide if the URL links to the Attachment or the Attachment Parent page.

3. Once done, don’t forget to click the Save Changes button.

#2 Add Code Snippet

If you feel comfortable in dealing with codes, try adding a code snippet to your WordPress.

  1. Create a new file in your WordPress theme folder and change the name to image.php. If your theme already has an image.php file, simply edit that file.
  2. Disable the attachment page by adding the following code to the first line of your image.php file.
<?php wp_redirect(get_permalink($post->post_parent)); ?>

Remember to save the image.php file. You need an FTP client or a WordPress hosting control panel to move the file to the theme directory.

Follow these steps and when users reach your image attachment page, they will be redirected to the main post.

It’s up to you to decide whether the attachment page is necessary or not. However, it is worth considering its benefits in some cases. The pages allow you to present valuable information about the images, changes you made, visitors’ comments, and more.

These juicy elements will help the site rank higher on Google if being used consistently and correctly.

Final Thoughts on WordPress Attachment Page

If you don’t have an attachment page yet, you can give it a try. In case attachment pages harm your site, simply disable them using the methods mentioned above. Or, check out our guide for other ways to disable WordPress attachment pages.

Besides disabling them, you can try redirecting WordPress attachment pages for better SEO.

What do you think about attachment pages? Leave some words in the comment section below if you still have any questions.