質問: '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; ?>
回答: あなたはwordpress関数get_childrenを使うことができます。パフォーマンス面では違いはありませんが。<?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; ?>