• Hi,

    I have an image slider on the main page, and it must only show 5 images from posts with featured image. (I have 33 posts, and only about 7 posts with a featured image).

    At the moment I’m using this :

    <?php $who_query = new WP_Query(array('cat' => '11', 'posts_per_page' => '-1')); ?>
    			<?php if($who_query->have_posts()) : while($who_query->have_posts()) : $who_query->the_post(); ?>
    			<?php if (has_post_thumbnail()) {
    				echo '<a href="' . get_permalink($post->ID) . '" >';
    				echo get_the_post_thumbnail($post->ID);
    				echo '</a>';
    			}
    			?>
    			<?php endwhile; endif;  ?>

    It works fine, but it shows all 7 featured images available.

    I know how I want it to work :

    1. Get the loop to go through all posts
    2. Get posts with featured image
    3. Then show only 5 of all the featured images

    But I don’t know how to get WordPress to find posts with images, but only show 5 out of those. If I change to 'posts_per_page=5, it will only grab the latest 5 posts THEN show featured images.

    Is there a way to grab all posts with featured image THEN show only 5 posts?

    Thanks 🙂

  • The topic ‘Show featured images from posts on main page’ is closed to new replies.