Here’s a possible solution. First a couple of caveats:
1. You need to be running a child theme. If you’re not, you can download one in Theme Options under the Help tab in the upper righthand corner.
2. This code will only work with images that are attached to the post; i.e. images that are Uploaded directly to the post from within the Media Manager. This is not the same as inserting media into the post content. The code does NOT work with images that are inserted into the post content.
3. I also tested this with a standard WP gallery and it pulled the image in the gallery with the lowest ID.
The changes below need to be applied to content.php. If you’re using the Standard Blog List they would also need to go into content-standard.php. I didn’t test this with Featured Posts but, if you’re using that option, you’d also need to edit content-featured.php.
Copy the content file to your child theme.
Locate the following block of code at the top of the file:
<?php if ( has_post_thumbnail() ): ?>
<?php the_post_thumbnail('thumb-medium'); ?>
<?php elseif ( ot_get_option('placeholder') != 'off' ): ?>
<img src="<?php echo get_template_directory_uri(); ?>/img/thumb-medium.png" alt="<?php the_title(); ?>" />
<?php endif; ?>
The logic above says:
– If there’s a featured image, show it
– Else, if the placeholder option is on, show it
– Else no image
We’re going to insert a new block of code after the featured image logic to check for images attached to the post. If there is one, or more, display the first one. Add the new block of code in the middle so the entire block looks like this:
<?php if ( has_post_thumbnail() ): ?>
<?php the_post_thumbnail('thumb-medium'); ?>
<?php else: ?>
<?php
// GET THE FIRST IMAGE ATTACHED TO THE POST OR THE LOWEST IMAGE ID IN A GALLERY.
// THIS ONLY WORKS FOR IMAGES THAT ARE UPLOADED TO THE POST IN THE MEDIA MANAGER, NOT INSERTED INTO THE POST CONTENT FROM THE MEDIA LIBRARY!!
$args = array(
'order' => 'ASC',
'orderby' => 'menu_order',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_status' => null,
'numberposts' => 1,
);
$img_attachments = get_posts($args);
?>
<?php if ($img_attachments): ?>
<?php foreach ($img_attachments as $attachment) {
echo wp_get_attachment_link($attachment->ID, 'thumb-medium', false, false);
} ?>
<?php elseif ( ot_get_option('placeholder') != 'off' ): ?>
<img src="<?php echo get_template_directory_uri(); ?>/img/thumb-medium.png" alt="<?php the_title(); ?>" />
<?php endif; ?>
<?php endif; ?>
Let me know how that works.