Photo gallery with Featured Images

First, I need to ask if it’s possible to create a photo gallery only with the featured images of the latest 6 posts? I assume it is possible but just want to make sure.

I don’t have much experience working directly with the wordpress gallery so could someone point me in the right direction for accomplishing this? Any plugin with similar functionally or tutorial will do.

Related posts

Leave a Reply

2 comments

  1. $query = new WP_Query(array('posts_per_page' => 6, 'meta_key' => '_thumbnail_id'));
    while($query->have_posts()) :
        $query->the_post();
        the_post_thumbnail();
    endwhile;
    

    This code gets 6 latest posts which have featured image available & show those 6 images on the page. It will skip those posts where there is no featured image set even if they are newer.

    This will probably get you started. You should read the codex pages for WP_Query & the_post_thumbnail

  2. As I just ran into that problem yesterday, here’s the solution: You’ll have to set numberposts as well as posts_per_page to cover every edge case scenario.

    I also included the possibility to query for attached files as well as a check if there’re any attachments.

    $wpse69856_query = new WP_Query( array(
         'posts_per_page' => 6
        ,'numberposts'    => 6
        ,'meta_key'       => '_thumbnail_id'
        // For non-featured images
        ,'meta_key'       => '_wp_attached_file'
    ) );
    if ( $wpse69856_query->have_posts() )
    {
        while( $wpse69856_query->have_posts() )
        {
            $wpse69856_query->the_post();
            the_post_thumbnail();
        }
    }
    

    Note: This is just an addition to @Mridul Aggarwal answer to make it more complete. In the case this works for you, please mark his answer as solution. Thanks.