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 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>';
        }
    }
}
?>
share|improve this question

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.