5

I'm trying to figure out how to count and display the number of images in a gallery with WP 3.5. I was following an old post on Ottopress, querying the DB for attachments but it seems the galleries are now contained in shortcodes.

Is it possible to extract the shortcode from the post and count the IDs? What is the best way to accomplish this?

2
  • Where do you need the number?
    – fuxia
    Jan 11, 2013 at 20:47
  • In the loop, similar to how ma.tt displays gallery posts.
    – javy
    Jan 12, 2013 at 8:52

4 Answers 4

2

This works:

$images = get_children( array(
    'post_parent' => $post->ID,
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'orderby' => 'menu_order',
    'order' => 'ASC',
    'numberposts' => 999
));
if ( $images ) {
    $total_images = count( $images );
}

The variable $total_images will hold the count of images in your gallery.

3
  • Not getting any results for this. In wp_posts in the DB, the only posts which are attachments that I'm seeing are Featured Images.
    – javy
    Jan 22, 2013 at 20:50
  • Are the images actually attached to the post, or are you just referencing them directly from the media library? The code works perfectly for me in all the themes I've used it in.
    – justmattb
    Jan 23, 2013 at 6:09
  • 1
    I'm using the native WP 3.5 gallery shortcode. It doesn't attach them to posts, it references the media IDs directly.
    – javy
    Jan 23, 2013 at 15:59
2

This worked well for me for a gallery post format... where is was safe to assume only all attachments were in 1 gallery. Doesn't seem totally relevant but thought I'd throw it out there.

$num_attachments = array_sum ( (array) wp_count_attachments('image') );
2

If you need count after gallery itself was processed, the light way might be to hook into post_gallery filter in gallery_shortcode() and note down explicitly included posts from arguments it passes.

It does get somewhat complicated with multiple galleries in same post, but it's hard to recommend specific approach without full context of your needs.

-2

Here I want to count images for each post inside the gallery, it was working fine for me. Put below code where you want to display total images:

<!-- ============== Count Images =============== -->
<?php $postid = get_the_ID();
if ( $postid !='' ) {
    $set     = ci_featgal_get_attachments();
    $i = 0;
    while ( $set->have_posts() ) : $set->the_post();
        get_template_part( 'loop'); $i++;
    endwhile; wp_reset_postdata(); ?>
    <h6 style="font-size:10px;color:#c9b07d;"><?php echo $i ." PHOTOS";?></h6>
<?php } ?>
<!-- ============== Count Images End =============== --> 
2
  • <?php if( $postid !='' ) {?> <?php $set = ci_featgal_get_attachments(); $i = 0; ?> <?php while ( $set->have_posts() ) : $set->the_post(); ?> <?php get_template_part( 'loop'); $i++;?> <?php endwhile; wp_reset_postdata(); ?> <h6 style="font-size:10px;color:#c9b07d;"><?php echo $i ." PHOTOS";?></h6> <?php } ?>
    – Vijay
    Sep 19, 2015 at 15:32
  • Your answer include custom functions and template parts that are not included. It is very unlikely that thé code works for anyone else except you.
    – cybmeta
    Sep 19, 2015 at 19:42

Your Answer

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

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