WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 1 down vote accepted

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){
  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

Your Answer


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.