Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I show the number of images attached to a post on the image attachment page?

I use the image attachment page to show images attached to a post one by one, in a slideshow sort of affect. I'd like to be able to display the total number of images attached to the parent post and the number of the particular image that's being shown on any given attachment page so you see the picture and the words "Image 3 of 15" for example.

Update... I was able to get the total number to show using this code:

<?php 
  global $post;
  $attachments = get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) );
  $count = count( $attachments );
  echo $count; 
?>

I still can't figure out how to show the number of the current image.
Anyone have any suggestions?

Update 2...

Greenie's answer got me almost there but it's outputting all the numbers at once:

"Image 1 of 8Image 2 of 8Image 3 of 8Image 4 of 8Image 5 of 8Image 6 of 8Image 7 of 8Image 8 of 8"

Here is the code I used:

global $post;
$attachments = get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) );
$count = count( $attachments );
$currentImage = 1;
foreach ($attachments as $attachment) {
   // output your image here
   echo "Image ". $currentImage . " of ". $count; 
   $currentImage++; 
}

What's going wrong?

Update 3 - THE ANSWER!

global $post;
$attachments = get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) );

$count = count( $attachments );
$specific = array();
$i = 1;

foreach ( $attachments as $attachment ) {
    $specific[$attachment->ID] = $i;
    ++$i;
}

echo "Image {$specific[$post->ID]} of {$count}";
like image 540
mattz Avatar asked Nov 15 '22 11:11

mattz


1 Answers

This works:

global $post;
$attachments = get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) );

$count = count( $attachments );
$specific = array();
$i = 1;

foreach ( $attachments as $attachment ) {
    $specific[$attachment->ID] = $i;
    ++$i;
}

echo "Image {$specific[$post->ID]} of {$count}";
like image 78
mattz Avatar answered May 26 '23 07:05

mattz