Title: dynamically get attachment id ?
Last modified: August 30, 2016

---

# dynamically get attachment id ?

 *  Resolved [mattegg](https://wordpress.org/support/users/mattegg/)
 * (@mattegg)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/dynamically-get-attachment-id/)
 * Firstly, this plugin is very useful, thanks for the hard work creating it.
 * Your notes say :
 * **$pdf_id = ‘your pdf file ID’;**
    if ( $thumbnail_id = get_post_meta( $pdf_id,‘
   _thumbnail_id’, true ) ) { echo ‘[‘.wp_get_attachment_image ( $thumbnail_id, ‘medium’ ).’](https://wordpress.org/support/topic/dynamically-get-attachment-id/&apos;.wp_get_attachment_url( $pdf_id ).&apos;?output_format=md)‘;}
 * Which is great, however, i want to use this information in a template file, and
   need to get the PDF file ID dynamically, each post will have a different file
   ID – how would i go about doing this?
 * [https://wordpress.org/plugins/pdf-image-generator/](https://wordpress.org/plugins/pdf-image-generator/)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Author [Mizuho Ogino](https://wordpress.org/support/users/fishpie/)
 * (@fishpie)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/dynamically-get-attachment-id/#post-6655746)
 * Hi, mattegg.
    I think there are mainly three ways.
 * 1. Set pdf file as featured image. The plugin allow you to use pdf as image. 
   You just need to call the featured image like below…
    `echo get_the_post_thumbnail(
   $post->ID, 'medium' );`
 * 2. Use custom field plugin like “Advanced Custom Field”. If ACF, Make your filed
   name and set “File type” to “file” / “Return Value” to “File ID”. Then, you call
   the ID like below…
 *     ```
       $pdf_id = get_post_meta( $post->ID, 'My_Field_Name', true ); // set your field name.
       if ( $thumbnail_id = get_post_meta( $pdf_id, '_thumbnail_id', true ) ) {
         echo wp_get_attachment_image ( $thumbnail_id, 'medium' );
       }
       ```
   
 * 3. If you call all PDFs that are attached to the post. You can get them by get_posts
   function.
 *     ```
       $pdfs = get_posts( 'posts_per_page=-1&post_type=attachment&post_mime_type=application/pdf&post_parent='.$post->ID );
       if( $pdfs ): foreach( $pdfs as $pdf ):
         $thumbnail_id = get_post_meta( $pdf->ID, '_thumbnail_id', true );
         if( $thumbnail_id ){
           echo wp_get_attachment_image ( $thumbnail_id, 'medium' );
         }
       endforeach; endif;
       ```
   
 * Hope you try them.
    Thanks.
 *  Thread Starter [mattegg](https://wordpress.org/support/users/mattegg/)
 * (@mattegg)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/dynamically-get-attachment-id/#post-6655785)
 * Great post, perfect answer. I tried the last option you gave and it worked perfectly.
   I will try the plugin you recommended as well.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘dynamically get attachment id ?’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/pdf-image-generator_c3bea9.svg)
 * [PDF Image Generator](https://wordpress.org/plugins/pdf-image-generator/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/pdf-image-generator/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/pdf-image-generator/)
 * [Active Topics](https://wordpress.org/support/plugin/pdf-image-generator/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/pdf-image-generator/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/pdf-image-generator/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [mattegg](https://wordpress.org/support/users/mattegg/)
 * Last activity: [10 years, 7 months ago](https://wordpress.org/support/topic/dynamically-get-attachment-id/#post-6655785)
 * Status: resolved