在WordPress库中获取总图像的“计数”

I m making a theme and want to add total image counts of a gallery in a single post. I did auto number images and want to show 1 of 4, 2 of 4 ... before each image.

function gallery_callback( $matches )
{

    $media = get_attached_media( 'image',  $post_id);
    $count2 = count($media);

    static $count = 0;
    $count += 1;

return "<span class='gallery-number'> $count $count2</span>" . $matches[0];

} 

This code show number before each image. And $count2 counts all images in post. But i want to count only gallery images. It shows all images attached to post ( thumbnail, related posts, widget images etc... )

How can i retrieve gallery image count of a post outside the loop.

https://resmim.net/f/131WmH.jpg

In the image first number shows auto number of images ( thats correct ) - Second number shows all images in post ( includes everything - actually there is 4 images in the gallery )

Thanks for help.

Because This code show number before each image. And $count2 counts all images in post. But i want to count only gallery images. It shows all images attached to post ( thumbnail, related posts, widget images etc... )