In previous installations of WordPress it was possible to number images in attachment.php
using the code below, which was originally provided here. However, I’m finding that this code no longer works as the image numbers are inaccurate.
Is there a new code available?
<?php
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));
?>
There is no complete solution possible. And that is not new.
Any image can be a part of a gallery in a post that is not that imageâs parent. So the post parent is not a useful parameter. The new interface makes such galleries easier to create, but they were possible in earlier versions too.
Even if the attachmentâs parent contains a gallery with that image â there could be another gallery with the same image. Yes, you can create multiple galleries and re-use images.
What you can do:
Alternative:
Create an endpoint for post galleries to keep the relation intact. With an URL like â¦
⦠you would know you are on the fourth attachment for the first gallery that belongs to the post with the slug
postname
.The images can be reordered by clicking and dragging to move them around. I wish that the external hyperlink option hadn’t been removed though!!