Home › Forums › General Issues › ACF file field: Getting generated images from PDF
Hello! I have set up an ACF file field. There I am adding PDFs. WordPress is now generated images from the first page of the pdfs. How can I alter this query to not print the icon, but the generated image of the pdf file. I want to use one of the smaller generated image sizes, not the full sized one.
Thanks very much!
<?php
$file = get_field('dokumente_datei');
if( $file ):
// Extract variables.
$url = $file['url'];
$title = $file['title'];
$caption = $file['caption'];
$icon = $file['icon'];
// Display image thumbnail when possible.
if( $file['type'] == 'image' ) {
$icon = $file['sizes']['medium'];
}
// Begin caption wrap.
if( $caption ): ?>
<div class="wp-caption">
<?php endif; ?>
<a href="<?php echo esc_attr($url); ?>" title="<?php echo esc_attr($title); ?>">
<img src="<?php echo esc_attr($icon); ?>" />
</a>
<?php
// End caption wrap.
if( $caption ): ?>
<p class="wp-caption-text"><?php echo esc_html($caption); ?></p>
</div>
<?php endif; ?>
<?php endif; ?>
// Get the ID of the attachment rather than the url, setting 3rd parameter
// to false will return unformatted value, intval is just in case a string
// is returned
$file_id = intval(get_field('dokumente_datei', false false);
// get the thumbnail
$image = get_attachment_image_src($file_id, 'thumbnail');
Thanks very much for your help!!!
I somehow get an 500 error, when I try to run your script above.
Think I am missing something here.
Syntax error on getting the field ID, missing closing )
When coding you should always enable debug display so that you get a useful error report rather than a WOD https://codex.wordpress.org/WP_DEBUG
$file_id = intval(get_field('dokumente_datei', false false));
Thanks again for your help. I am pretty new with all this.
I have now tried around several attempts, but I still get an error:
Parse error: syntax error, unexpected ‘false’ (T_STRING), expecting ‘)’ in (…)/wp-content/plugins/oxygen/component-framework/components/classes/code-block.class.php(115) : eval()’d code on line 2
<?php if( get_field('dokumente_datei') ):
$file_id = intval(get_field('dokumente_datei', false false));
$image = get_attachment_image_src($file_id, 'thumbnail');
?>
<img src="<?php echo esc_attr($image); ?>" />
<?php endif; ?>
Your help is highly appreciated!
I have got this somehow to work:
<?php
$text = get_field('dokumente_datei');
$stripped_text = str_replace(".pdf","-pdf.jpg","$text");
?>
<img src=<?php echo $stripped_text; ?>" width="100%">
But it returns the full-size image and I need a smaller one for the post loop. As the smaller ones are named differently (pixel size added to the image names), it does not work either as I try to get it to work….
I have finally found a working solution:
<?php
$file = get_field('dokumente_datei');
$url = $file['url'];
$image_id = $file['id'];
$image_url = wp_get_attachment_image($image_id,'medium');
if( $file ):
?>
<a href="<?php echo $file['url']; ?>">
<?php echo ($image_url); ?></a>
<?php endif; ?>
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.