I have an attachment page for post images, so that when I click on the thumbnail (on the front end) it takes the user to the attachment page for that image, how would I do the same for pdf files, so that when I click on the direct link to the pdf on the front end to redirects to the pdf attachment page?

<?php if ( wp_attachment_is_image( $post->id ) ) : $att_image = wp_get_attachment_image_src( $post->id, "full"); ?>
    <p class="full-attachment">
        <?php /* <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-full" alt="<?php $post->post_title; ?>" />
        <?php /* </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) ?>
<?php endif; ?> 

This is my theme file's single.php

<?php $file= get_post_meta( $post->ID, 'teacher-resume-upload' );
 if ( $file) { foreach ( $file as $attachment_id ) { $full_size = wp_get_attachment_url( $attachment_id ); printf( '<a href="%s">download</a>', $full_size); } } ?>

3 Answers 3


You can use pdf.php or a number of other specially-named template files as described birgire's answer to show an attachment page specifically for PDFs, or use a generic attachment.php page that includes conditional statements for the type of attachment (like the code in your question does).

Potential Conflict with Yoast SEO Plugin

Note that attachment template pages may not work if you have the Yoast SEO plugin installed. The default setting under Yoast SEO › Search Appearance › Media is to 'Redirect attachment URLs to the attachment itself,' which will override any attachment page template and directly link to the PDF, image file, or whatever you've uploaded.

How to Use Attachment Page Only for PDFs and Redirect Other Attachment Types Directly to their Files

Bonus: If you only want to show PDF attachment pages and redirect everything else to the attachment file, here's some code to do that. (You'd put this in your functions.php file.)

 * Redirect media attachment URLs to the attachment itself *unless* it's a PDF.
 * See details: https://wordpress.stackexchange.com/questions/253226/
  function() {
    // Exit if this is not an attachment page.
    if ( ! is_attachment() ) {

    // Exit (do nothing; do not redirect; use
    // the pdf.php attachment template page) 
    // if this is a PDF attachment page.
    if ( false !== stripos( get_post_mime_type(), 'pdf' ) ) {

    // Redirect all other attachment pages to
    // their file. Use code '301' to indicate
    // this is a permanent redirect.
    if ( wp_get_attachment_url() ) {
      wp_safe_redirect( wp_get_attachment_url(), '301' );

From the get_attachment_template() we can see how the attachment's template hierarchy is constructed.

Let's check out some examples:

  • For a pdf file, with the applicaton/pdf mime type it's:

    - application-pdf.php
    - pdf.php
    - application.php
    - attachment.php
  • For a jpeg image with the image/jpeg mime type it's:

    - image-jpeg.php
    - jpeg.php
    - image.php
    - attachment.php
  • For a png image with the image/png mime type it's:

    - image-png.php
    - png.php
    - image.php
    - attachment.php
  • For an mp3 audio file with the audio/mpeg mime type it's:

    - audio-mpeg.php
    - mpeg.php
    - audio.php
    - attachment.php

If none of these files exists in the current theme directory, then the fallbacks are (in order):

- {custom post template}.php
- single-attachment-{slug}.php
- single-attachment.php
- single.php
- singular.php
- index.php

I added the {custom post template}.php here, because we can add a postmeta row for the _wp_page_template key for a given attachment, with the template filename as a meta value.

If we check out the Codex on Template Hierarchy, then it seems to be missing some fallback options.

Then if we want to modify the embed template for attachments, then we can e.g. use embed-attachment.php or embed.php

  • Great thanks for that, I'll try to copy and modify my image attachment template.
    – Pete
    Jan 20, 2017 at 12:30
  • you're welcome, hope it worked for you @Pete
    – birgire
    Feb 17, 2017 at 15:52
  • Nope it didn't unfortunately
    – Pete
    Feb 17, 2017 at 15:52
  • strange, I just created the pdf.php file under the TwentySixteen theme and it displays as my PDF attachment page @Pete
    – birgire
    Feb 17, 2017 at 15:58
  • Not strange, I'm sure you're right. I'm just not smart enough to work out how to adapt my attachment.php file (see above).
    – Pete
    Feb 17, 2017 at 16:02

This should do the trick:

<?php if ( get_post_mime_type($post->ID) == 'application/pdf' ) : ?>
    <object data="<?php echo wp_get_attachment_url($post->ID); ?>" type="application/pdf" width="100%" height="1000px"><a href="<?php echo wp_get_attachment_url($post->ID); ?>">Download the PDF here.</a></object>
<?php endif; ?>

get_post_mime_type() tells your what the type of your attachment is - kinda like the wp_attachment_is_image($post->ID). If it matches PDF then the code is being executed.

When you run this outside of the attachment.php template file you should wrap the code in <?php if is_attachment(): ?> <?php endif; ?>.

  • It doesn't show any link in my single.php
    – Pete
    Feb 17, 2017 at 17:47
  • In another comment you are mentioning that you want to adapt it to your attachment.php. This code should go in attachment.php. Feb 17, 2017 at 19:45
  • Where is relation to my above example?
    – Pete
    Feb 17, 2017 at 19:50
  • So many google pages before I found this thanks!
    – Joe
    Jan 10, 2020 at 12:10

Your Answer

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

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