I just wonder why WP_Query adding one more additional attachment with the defined number or posts. Also need to link attachment (thumbnail) to the post url. Here is my code
<?php
$query = new WP_Query( array( 'post_type' => 'news', 'showposts' => 5, 'orderby' => 'rand' ) );
if( $query->have_posts() ){
while($query->have_posts()){
$query->the_post();
$image_query = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'showposts' => -1, 'post_parent' => get_the_ID() ) );
while( $image_query->have_posts() ) {
$image_query->the_post();
echo '<div class="attachment">';
echo wp_get_attachment_image( get_the_ID() );
echo '</div>';
}
}
}
?>