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?

4 s
4

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.

Leave a Reply

Your email address will not be published. Required fields are marked *