0

I was trying to get a solution for something that I am trying to accomplish. I am using the wordpress native and simple gallery to show images in a page called: "gallery" so far - very simple. it works fine and I have a gallery page with thumbnails.

Now, I would like to show 8 latest photo thumbnails in my homepage. no link to the bigger image is necessary, just literally, grab 8 (or another number..) latest images which was added to that gallery and show them in a simple box in my homepage.

I would also like to output my own HTML so I can customize it to my homepage needs.

anyone has a solution? Thanks a lot.

2 Answers 2

2

Displaying images from your galleries is going to be labor intensive. "Galleries" are saved as shortcodes in the post body so you would need to:

  1. Query your database for posts containing a gallery shortcode
  2. Process the post content of the result set to extract the ids of the gallery images
  3. And then use those ids to retrieve the images themselves.

There are a couple of queries involved and one is a LIKE query on post content. It won't be especially fast. On the other hand...

Getting attachments is easy (gallery images are attachments but not all attachments are gallery images):

$args = array(
    'post_type' => 'attachment',
    'post_status' => 'inherit',
    'posts_per_page' => 10, // however many you want
);
$atts = new WP_Query($args);

And so is getting a set of thumbnails (featured images):

$thumbs = new WP_Query(
  array(
    'posts_per_page' => 10, // however many you want
    'meta_query' => array(
      array(
        'key' => '_thumbnail_id',
        'compare' => 'EXISTS'
      )
    ),
  )
);

I would recommend one of those two options.

2
  • Thanks @s_ha_dum, I was using your first function as I would like to display images from the gallery and not "featured images" but it is not working. I put it in my homepage template and it is not showing anything I also dont understand how can it display images from my gallery where there is no reference to the gallery page ID am I in the right direction?
    – gil hamer
    Nov 21, 2013 at 7:24
  • That code won't display anything. It is just a query. You need to add a Loop to display things. Read the answer again. "galleries" are saved as shortcodes in the post body. The actual images are attachments. But my code does not specifically pull galleries. As stated, I that is a labor intensive process.
    – s_ha_dum
    Nov 21, 2013 at 15:58
0

So I ended up creating this code:

<?php
$args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => 'any', 'post_parent' => 34, 'posts_per_page'   => 8 ); 
$attachments = get_posts( $args );
if ( $attachments ) {
    foreach ( $attachments as $post ) {
        setup_postdata( $post );
        echo '<div class="col-md-3 col-sm-3 col-xs-6">';

        $imageThumb = wp_get_attachment_image_src( $attachment->ID, 'full' );
        echo '<img class="img-thumbnail" src="'; echo $imageThumb[0]; echo '"/> '; 

        echo '</div>';
    }
    wp_reset_postdata();
}
?>

which basically reference the gallery page ID I want to pull the images from (ID 34) and limit that to 8 images.

Hope it helps anyone and thanks for the help @s_ha_dum

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.