Support » Plugin: PDF Thumbnails » How to implement PDF thumbnail

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Contributor mirgcire

    (@mirgcire)

    Okay … I found some clues in one of the reviews. The review linked to a thread where you suggested that this would work.

    <?php $attachment_id = get_field('file'); ?>
    <div class="thumb">
        <?php echo get_the_post_thumbnail($attachment_id, 'medium'); ?>
    </div>

    What is get_field() … the only documentation I can find for this function is in ACF seems like it is not a generic worpress function. Does $attachment_id contain the post ID of the thumbnail? If so my code above does the same thing.

    Any help would be much appreciated. Thanks

    Plugin Contributor mirgcire

    (@mirgcire)

    Oops, I forgot to include my code.

    function post_thumbnail( $atts, $content = null ) {
    	extract( shortcode_atts( array('post-id' => '0'), $atts ) );
    	$post_id = $atts['post-id']);
    	return '<div id="pdf_thumbnail">' . get_the_post_thumbnail($post_id) . '</div>';
    }

    It is getting the correct post code, but it doesn’t work. Also, it doesn’t work if I add ‘medium’ as a parameter.

    Plugin Contributor mirgcire

    (@mirgcire)

    Well, I discovered something interesting … I opened the URL associated with an uploaded PDF and it automatically opened in a viewer. So it seems like a should be able to create an a-link tag with pdf-thumbnail as the image and assign the PDF URL to the href.

    Plugin Contributor mirgcire

    (@mirgcire)

    Nice! … that worked reasonably well

    function view_pdf ($atts, $content = null) {
    	extract( shortcode_atts( array ('file' => '','post' => ''), $atts ) );
    	$file = 'http://newtheme.effectivechinese.net/wp-content/uploads/'.$atts['file'];
    	$post = $atts['post'];
    	$image = get_the_post_thumbnail($post);
    	return '<a href="'.$file.'">'.$image.'</a>';
    }
    add_shortcode ("view-pdf", "view_pdf");

    but it would be nice if I could extract the pathname from the post id.

    Plugin Contributor mirgcire

    (@mirgcire)

    Finally …

    function view_pdf ($atts, $content = null) {
    	extract( shortcode_atts( array ('post' => ''), $atts ) );
    	$post = $atts['post'];
    	$image = get_the_post_thumbnail($post);
    	$file = get_attached_file ($post);
    	$url = 'http://myrocketscience.com/'.substr($file, strpos($file, "wp-content"));
    	return '<a href="'.$url.'">'.$image.'</a>';
    }
    add_shortcode ("view-pdf", "view_pdf");

    And a relatively painless short code: [view-pdf post=”7601″]

    Now if I could only get a smaller thumbnail.

    Plugin Author stianlik

    (@stianlik)

    Seems like you have figured out most the details on your own 🙂

    You can simplify your code using wp_get_attachment_url instead of get_attached_file to get the URL directly.

    The size can be modified as follows:

    1. Register thumbnail size (e.g. my-small-size) using add_image_size (unless the built-in WordPress sizes suit your needs: ‘thumb’, ‘thumbnail’, ‘medium’, ‘large’, ‘post-thumbnail’)

    2. Change $image = get_the_post_thumbnail($post); into $image = get_the_post_thumbnail($post, 'my-small-size');

    Plugin Contributor mirgcire

    (@mirgcire)

    Thanks! … here is my final version

    function view_pdf ($atts, $content = null) {
    	extract( shortcode_atts( array ('post' => '', 'size' => ''), $atts ) );
    	$post = $atts['post'];
    	$size = $atts['size'];
    	$image = get_the_post_thumbnail($post, $size);
    	$url = wp_get_attachment_url ($post);
    	return '<a href="'.$url.'">'.$image.'</a>';
    }
    add_shortcode ("view-pdf", "view_pdf");

    Works like a champ! Feel free to add this feature to your plugin.

    Plugin Author stianlik

    (@stianlik)

    Thanks! I have added the functionality to version 2.1.0, see the plugin page for documentation.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to implement PDF thumbnail’ is closed to new replies.