Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Create a Custom Single Attachments Template in WordPress

Last updated on by
Elegant Themes
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:

<?php
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>
                        </p>
<?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; ?>
                        </div>

<?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.

Resource:
Template Hierarchy Codex


Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »

Comments

  1. Rita says:

    This code worked like a dream – thank you so much!

  2. Agnes says:

    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?

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.