Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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".

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted
+50

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));
share|improve this answer
    
Is there a way to reverse which numbers are assigned to the images? For example, 1 of 4 should actually be 4 of 4. Take a look: themeforward.com/demo2/features/gallery-2 –  AndrettiMilas Dec 14 '11 at 22:30
    
try adding $photos = array_reverse($photos); –  onetrickpony Dec 15 '11 at 11:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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