You can use has_post_thumbnail() to check for a featured image.
If that returns false, then you can use get_children() to retrieve images attached to the Post (N.B. that isn’t quite the same thing as being included in the post content).
First load the attachments into a variable with $imageAttachments = get_children('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image')
, then check whether $imageAttachments is empty and act accordingly (if it is show a default image, and if it isn’t display the first attached image at the appropriate size).
Of course, you’ll also need to define the thumbnail size (see add_image_size()).
Tim
In the end I used get_posts, because I got confused, and thus irritated by the documentation for get_children().
<?php
$show=9;
$r = new WP_Query(array('showposts' => $show, 'what_to_show' => 'posts', 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1));
if ($r->have_posts()) :
while ($r->have_posts()) : $r->the_post(); ?>
<div class="upcoming" style="margin-bottom:25px; padding:0 5px 5px 20px;">
<?php $args = array(
'post_type' => 'attachment',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
?> <a style="border:none;" href="<?php the_permalink() ?>">
<?php echo wp_get_attachment_image($attachment->ID, 'front-thumb'); ?>
</a> <?php
}
}?>
<p><a style="color:red; text-decoration:none; text-align:left;" href="<?php the_permalink() ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?> </a></p>
</div>
<?php endwhile; ?>
<?php
wp_reset_query(); // Restore global post data stomped by the_post().
endif; ?>
One thing to watch with that: Because you haven’t specified a ‘post_mime_type’, get_posts could retrieve an attached PDF, which would mess things up.
Tim
If you want to use get_children() in category, you will need to add “attachement” to your post_type for your category.
[Stop posting your site url all over the place!]
Hello
I really need this feature – can someone please tell me where to cut and paste this code into
[code moderated - please follow the forum guidelines for posting code]