Skip Navigation

[Resolved] Trying suggestions for PDF thumbnails, not working

This support ticket is created 3 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 11 replies, has 2 voices.

Last updated by Beth 3 years, 6 months ago.

Assisted by: Shane.

Author
Posts
#1811289

Tell us what you are trying to do?
I'm trying to implement the accepted suggestion for displaying PDF thumbnails in a view loop.
I expect to see a grid of medium-sized PDF thumbnail images linking to the corresponding single page.
Instead, I get a grid of links with the raw text of the shortcode as the link [getpublicationpdfimg size=”medium”]
I did add the code to my child theme's functions.php (I use GeneratePress) and replaced the publication_document string with the slug of the file field, as indicated, which is called journalpdf.

Is there any documentation that you are following?
https://toolset.com/forums/topic/how-to-display-thumbnails-of-of-pdfs-uploaded-to-the-types-document-field/

Is there a similar example that we can see?
I haven't found one. Any external links in the forum posts are all hidden, so I can't visit examples.

What is the link to your site?
hidden link

#1811367

Here is my code:
In the functions.php of my child theme, as copied from the accepted solution:

function getPDFimg($atts) {
             
             extract( shortcode_atts( array(
            'size'=>'full',
        ), $atts ) );
             
            $image_url = str_replace(".pdf", ".jpg", types_render_field( "journalpdf"));
            $dirname = str_replace('.jpg','',str_replace(home_url('/'),'',$image_url));
            $images = glob($dirname."*.jpg");
            if(isset($images[0])){
                        $small_image = home_url('/').$images[0];
            }
            if(isset($images[1])) {
                        $medium_image = home_url('/').$images[1];
            }
            if(isset($images[2])) {
                        $large_image = home_url('/').$images[2];
            }
             
            switch($size) {
                        case 'small':
                                    if(isset($small_image) && $small_image != '' ) {
                                                $image_src = $small_image;      
                                    }
                                    else{
                                                $image_src = $image_url;
                                    }
                        break;
                         
                        case 'medium':
                                    if(isset($medium_image) && $medium_image != '' ) {
                                                $image_src = $medium_image;  
                                    }
                                    else{
                                                $image_src = $image_url;
                                    }
                        break;
                         
                        case 'large':
                                    if(isset($large_image) && $large_image != '' ) {
                                                $image_src = $large_image;      
                                    }
                                    else{
                                                $image_src = $image_url;
                                    }
                        break;
                         
                        default:
                                    $image_src = $image_url;
                                                 
            }
             
    return '<img src=' .$image_src  . ' class="pubdoc-pdf-image"> ';
}
  
add_shortcode('getpublicationpdfimg', 'getPDFimg');    

And in an HTML block of my Content Template View Loop in HTML mode, not visual.

<a href="[wpv-post-url]">[getpublicationpdfimg size="medium"]</a>
#1812563

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Beth,

Thank you for getting in touch. It should be noted that we do not support custom coding even if it was provided by one of our supporters.

However from what I can see it is your shortcode that is not rendering. If you've added it to your functions.php file then it should work.

Can you retype the shorcode name manually as well as the quotations for the medium size outside of the anchor tag to see if this works.

I can see on your frontend that the quotes that are being rendered are not correct.

Thanks,
Shane

#1812759

I've played with the code and I actually have a path to the image now, but it is rendering as a text link, not as an image.
hidden link

This is what I get in the HTML on the page:

<a href="<em><u>hidden link</u></em>" title="<em><u>hidden link</u></em>"><em><u>hidden link</u></em>;

This is the functions.php

function getPDFimg($atts) {
              
             extract( shortcode_atts( array(
            'size'=>'full',
        ), $atts ) );
              
            $image_url = str_replace(".pdf", "-pdf-image.jpg", types_render_field( "journalpdf"));
            $dirname = str_replace('-pdf-image.jpg','',str_replace(home_url('/'),'',$image_url));
            $images = glob($dirname."*-pdf-image.jpg");
            if(isset($images[0])){
                        $small_image = home_url('/').$images[0];
            }
            if(isset($images[1])) {
                        $medium_image = home_url('/').$images[1];
            }
            if(isset($images[2])) {
                        $large_image = home_url('/').$images[2];
            }
              
            switch($size) {
                        case 'small':
                                    if(isset($small_image) && $small_image != '' ) {
                                                $image_src = $small_image;      
                                    }
                                    else{
                                                $image_src = $image_url;
                                    }
                        break;
                          
                        case 'medium':
                                    if(isset($medium_image) && $medium_image != '' ) {
                                                $image_src = $medium_image;  
                                    }
                                    else{
                                                $image_src = $image_url;
                                    }
                        break;
                          
                        case 'large':
                                    if(isset($large_image) && $large_image != '' ) {
                                                $image_src = $large_image;      
                                    }
                                    else{
                                                $image_src = $image_url;
                                    }
                        break;
                          
                        default:
                                    $image_src = $image_url;
                                                  
            }
              
    return '' .$image_src  . '';
}
   
add_shortcode('getpublicationpdfimg', 'getPDFimg');
#1812871

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Beth,

Try doing it like this.


<a href="[wpv-post-url]"><img src="[getpublicationpdfimg size='medium']" /></a>

Thanks,
Shane

#1812891

This got me closer, thanks for helping me. For some reason, the img code is getting messed up in between the a tag and it's breaking the image. It's adding an errant </a> tag right after the image url, and before the quote. Why would it be doing this?

<a href="<em><u>hidden link</u></em>">
<img src="//slaafws-staging.hgfsqrd2-liquidwebsites.com/wp-content/uploads/2014_1_146aftersoberdating.jpg</a>">
</a>
#1812893

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Beth,

Not sure why it would be adding this at the end since in the code itself its not being used.

Would you mind allowing me to have admin access to check on this for you.
Thanks,
Shane

#1812915

To be clear, some quotes are changing to the HTML code for a quotation mark, looking at the source code when I inspect in Chrome. In particular, the quotes around the class="pubdoc-pdf-image" in the Template code.

#1813009

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Beth,

As far as I can see the code is working correctly, however there is something that is converting the URL that is being returned into a link already, at this point i'm not sure what is causing this but it is not coming from the custom code itself.

I would recommend turning off all your non-toolset plugins as well as checking any other custom code on your site to see what could be causing this.

Given that this is custom code there is not much that I can do with this.

Thanks,
Shane

#1813763

Thank you for trying, I appreciate it. I will disable some plugins and see if I can find the culprit here. I will post back if and when I find it in case others have this issue.

#1813889

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Beth,

Looking forward to hearing your response.

Hopefully its a plugin conflict of some sort because I wouldn't be able to assist in modifying the code.

Thanks,
Shane

#1818115

I ended up finding a script that automatically sets the featured image with the thumbnail attachment of the PDF when the post is saved. The 'journal-issues' portion would need be changed to your custom post type slug, this goes in your theme's functions.php. Then I just used the standard button for the featured image url to call the images for the loop. It's funny that WordPress generates these thumbnails automatically now, and yet makes it so hard to actually use them in any kind of automated way.

function save_pdf_thumb_as_featuredimage ( $post_id ) { 
    if ( wp_is_post_revision( $post_id ) ) return; 
    if ( get_post_type( $post_id ) !== 'journal-issues' ) return; // set your post type
    if ( get_post_meta( $post_id, '_thumbnail_id', true ) ) return; // post already has featured image
    $attaches = get_posts ( 'post_parent='.$post_id.'&numberposts=-1&post_type=attachment&post_mime_type=application/pdf&orderby=menu_order&order=ASC' );
    if ( $attaches ): foreach( $attaches as $attach ):
        if ( $thumb_id = get_post_meta( $attach->ID, '_thumbnail_id', true ) ){ // if pdf has thumbnail
            update_post_meta( $post_id, '_thumbnail_id', $thumb_id );
            break;
        }
    endforeach; endif;
}
add_action( 'save_post', 'save_pdf_thumb_as_featuredimage' );

Thanks for your help!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.