Instead of using get_post_gallery_images()
and attachment_url_to_postid()
, you can just use get_post_gallery()
which when the second parameter is false
, gives you an array with two items/keys: 1) src
— an array of image URLs, and 2) ids
— the attachment IDs (comma-separated).
So you can do $images_gallery = get_post_gallery( $section_content, false );
and then:
<div class="swiper-container post-slider" style="height:300px;">
<div class="swiper-wrapper">
<?php if ( $images_gallery ) :
$ids = wp_parse_id_list( $images_gallery['ids'] );
foreach ( $images_gallery['src'] as $i => $image ) :
$image_id = $ids[ $i ];
//get the image "post" information
$attached_image = get_post( $image_id );
// $image contains the image URL for the specified image size (e.g. 'large'),
// but you can really use functions like wp_get_attachment_image_src() to get
// a different image URL.
// ... your code.
?>
... your HTML
<?php
endforeach;
endif; ?>
</div>
<div class="swiper-scrollbar"></div>
</div>
UPDATE
So in reply to your comment:
I want to understand why WordPress will append the image size to the
link only for some images.
Because those some images are larger (in the dimension) than those that didn’t get appended with the image size. I.e. When you upload an image, WordPress would make a resized copy of the image for each defined image sizes in your site — the default sizes are thumbnail (150px x 150px
), medium (300px x 300px
) and large (1024px x 1024px
) as you can see on the media settings page (Settings → Media). And each of the image copies are named with the dimension appended at the end (<original name>-<resized width>x<resized height>.<ext>
) like the my-image-1024x683.jpg
.
So for example, if you uploaded an image with the dimension being 620px
(width) × 430px
(height), then WordPress would only create two resized images, one for the thumbnail size and another for the medium size. WordPress wouldn’t create the ‘large’ image because the original image is smaller than the ‘large’ size.
And if for example you put in your post content, where one of the images is the above example image (
620px x 430px
), WordPress would try to find the ‘large’ image URL for each of the gallery images and when not found, then the original image URL (e.g. my-image.jpg
) would be used.
And in the question, you asked:
As I can understand to get the ID is needed an URL like this:
http://myasite.com/wp-content/uploads/2020/03/my-image.jpg
. Is there
a fix?
Yes, you need the original image URL or path relative to the uploads
folder. And that’s because WordPress saves the path in a post meta named _wp_attached_file
, and attachment_url_to_postid()
would run a SQL command that looks like:
SELECT post_id FROM wp_postmeta
WHERE meta_key = '_wp_attached_file'
AND meta_value="2020/03/my-image.jpg"
But about that “Is there a fix?“, no, there’s nothing to fix here. But if you want to make your own function for getting the attachment ID from the resized image URL, then that’s up to you.
However, if you just want to retrieve the ID of the images in the post gallery (shortcode), then as I already mentioned, use get_post_gallery()
. It’s easy. 🙂