Gallery Image Numbers?

Is there a way, in an image gallery, to display what number the image is in the gallery in the image.php template? I’d like to incorporate this into my next / previous button area.

For example, if I’m on the first image out of twenty it would say “1 of 20”.

Related posts

Leave a Reply

1 comment

  1. This should do it:

    global $post;
    $ancestors = get_post_ancestors($post->ID);
    
    $photos = get_children(array(
      'post_mime_type' => 'image', 
      'post_parent'    => array_shift($ancestors),
    ));
    
    $index = 0;
    foreach($photos as $photo){
      $index++;
      if($photo->ID === $post->ID) break;
    }
    
    printf('%d out of %d', $index, count($photos));