Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm seeking to customize my categories listing (archive.php) so that it shows a thumbnail image of the first image attached to each post

However, apparently the archive.php file is one of those that does not natively support the attachment object. For example, the code below will do most of what I want (though if no attachment is found, I get a blank image, I need to fix that).

However, I'm afraid having a SELECT in a loop like this is perhaps way too expensive for what I'm trying to do.

Any ideas?

    <?php while (have_posts()) : the_post(); ?>
    <?php global $wpdb; $attachment_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post->ID' AND post_status = 'inherit' AND post_type='attachment' ORDER BY post_date DESC LIMIT 1"); ?>
        <div class="searchItem" style="clear:both;">
            <h3 id="post-<?php the_ID(); ?>"><img src="<?php echo wp_get_attachment_url($attachment_id); ?>" class="post-attachment" /><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
            <small><?php the_time('l, F jS, Y') ?></small>
            <div class="excerpt"><?php echo $post->post_excerpt; ?></div>
            <div class="postmetadata">Posted in <?php the_category(', ') ?> | <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></div>
        </div>
    <?php endwhile; ?>
share|improve this question
    
Any reason why you wouldn't want to use the_post_thumbnail in this instance? –  Chris_O Sep 8 '10 at 20:30
    
None, other than I can't get it to work with archive.php :) –  Scott B Sep 8 '10 at 21:09
    
Any template should be able to use the featured image as long as it called in the loop <?php the_post_thumbnail(); ?> Maybe you could edit your question and also include the code from your archive.php template. –  Chris_O Sep 8 '10 at 21:41
    
Chris, can you clarify this? I'm adding my images using the image attachment uploader at the top of the post editor. So these images are all "attached" to the post. Are you referring to the "featured" image or the "attached" images? I'm using "attached", not featured, assuming there is a difference in how they are called. –  Scott B Sep 8 '10 at 22:35
    
I am referring to the "featured" image. –  Chris_O Sep 9 '10 at 1:41

2 Answers 2

up vote 2 down vote accepted

You could use the WordPress function get_children. Although I don't think it makes a difference, performance-wise.

<?php while (have_posts()) : the_post(); ?>
    <?php $attachment = array_values( get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'numberposts'  => 1 ) ) ); ?>
    <div class="searchItem" style="clear:both;">
        <h3 id="post-<?php the_ID(); ?>">
        <?php if( $attachment ) echo '<img src="' . wp_get_attachment_url($attachment[0]->ID) . '" class="post-attachment" />'; ?>
        <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
        <small><?php the_time('l, F jS, Y') ?></small>
        <div class="excerpt"><?php echo $post->post_excerpt; ?></div>
        <div class="postmetadata">Posted in <?php the_category(', ') ?> | <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></div>
    </div>
<?php endwhile; ?>
share|improve this answer

WP has core function to view this, see my post http://wpengineer.com/1735/easier-better-solutions-to-get-pictures-on-your-posts/

share|improve this answer
    
Frank you should try to include at least some hint at what functions/code people should use. Linking to your post as backup is a good idea, but its a very long post, so a summary version in your answer is a good idea. –  jeremyclarke Sep 11 '10 at 21:09
    
Jeremy - i understand your hint, ok - but in most topics it is better when you understand the complex possibilities to find an solution. –  bueltge Sep 14 '10 at 7:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.