Getting first thumbnail from post – bug with wp code (?)
-
I’m working with some code that I found here on the forums to pull the first thumbnail from a post. I’ve actually tried two different methods, but the more concise method is this:
<?php
$images =& get_children( ‘post_type=attachment&post_mime_type=image&post_parent=’ . $post->ID );
$firstImageSrc = wp_get_attachment_image_src(array_shift(array_keys($images)), ‘thumbnail’);
if (empty($firstImageSrc)) { $firstImageSrc[0]=get_template_directory_uri().’/images/nopreview.gif’; }
?>The issue that I’m having is that MOST of the time, the function(s) works and I see a preview image… but sometimes I don’t.
I’ve confirmed the following:
– the thumbnail image does exist
– it’s not a strange file format (or any different file format than any of the working ones)
– it’s been uploaded the same way as all the others.I tried an alternate function for generating the thumbnail and came back with the same results.
As near as I can figure I can trace it back to the wp_get_attachment_image_src() function (which was used in both methods) but I’m not familiar enough with the hardcore wordpress innards to troubleshoot further.
Anyone else had a similar issue or have any suggestions, or even a work around that does not rely on that wp function?
Thanks in advance.
- The topic ‘Getting first thumbnail from post – bug with wp code (?)’ is closed to new replies.