Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Mizuho Ogino

    (@fishpie)

    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

    (@mattegg)

    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.