最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I make an attachment page for pdf uploads?

programmeradmin3浏览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); } } ?>

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); } } ?>
Share Improve this question edited Feb 18, 2017 at 1:32 Pete asked Jan 20, 2017 at 1:35 PetePete 1,0582 gold badges14 silver badges40 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 1

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/
 */
add_action(
  'template_redirect',
  function() {
    // Exit if this is not an attachment page.
    if ( ! is_attachment() ) {
      return;
    }

    // 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' ) ) {
      return;
    }

    // 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

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

发布评论

评论列表(0)

  1. 暂无评论