0

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"); ?>
    <center>
    <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> */ ?>
    </p>
    </center>
<?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; ?> 

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); } } ?>
| improve this question | | | | |
1

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

| improve this answer | | | | |
  • Great thanks for that, I'll try to copy and modify my image attachment template. – Pete Jan 20 '17 at 12:30
  • you're welcome, hope it worked for you @Pete – birgire Feb 17 '17 at 15:52
  • Nope it didn't unfortunately – Pete Feb 17 '17 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 '17 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 '17 at 16:02
1

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

| improve this answer | | | | |
  • It doesn't show any link in my single.php – Pete Feb 17 '17 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. – Marinus Klasen Feb 17 '17 at 19:45
  • Where is relation to my above example? – Pete Feb 17 '17 at 19:50
  • So many google pages before I found this thanks! – Joe Jan 10 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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