最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

theme development - Get gallery images description not work for some images id

programmeradmin2浏览0评论

I'm using this code to get the images from a post or a page to use them inside a custom slider.

<?php $section_content = get_page_by_path( 'partners', OBJECT, 'post' ); ?>
<?php $images_gallery = get_post_gallery_images( $section_content->ID ); //var_dump( $images_gallery ); ?>

      <div class="swiper-container post-slider" style="height:300px;">
        <div class="swiper-wrapper">
          <?php if( $images_gallery ): foreach( $images_gallery as $image ): ?>
          <?php  //get the id of the image post.
             $image_id = attachment_url_to_postid( $image );
             var_dump( $image_id );
             //get the image "post" information
             $attached_image = get_post( $image_id );
             //get the image title
             $image_title = $attached_image->post_title;
             //get the image caption
             $image_caption = $attached_image->post_excerpt;
             //var_dump($image_id, $attached_image);
            ?>
            <div class="swiper-slide" style="background-image:url('<?php echo $image; ?>');background-size:cover;">
              <!-- <img class="img-fluid w-100" src="" > -->
              <small class="text-white m-4 d-none d-md-block" style="position:absolute;top:0;"><?php echo $image_title; ?></small>
            </div>
          <?php endforeach; ?>
          <?php endif; ?>
        </div>
        <div class="swiper-scrollbar"></div>
      </div>

After searching here, I've found a quick solution to obtain the gallery images info like captions, description and title, but I have a problem with the title and the id. Some images id are not returned and if I use var_dump() I can see that the id value returned from the function attachment_url_to_postid() is int(0).

I discovered that wordpress will load an image that at the end of the url has the dimension appended. I've checked the images permalink in the media library but the url is ok and didn't has the dimension appended like the one that is used to display the file: .jpg. As I can understand to get the id is needed an url like this: .jpg.
Is there a fix?

I'm using this code to get the images from a post or a page to use them inside a custom slider.

<?php $section_content = get_page_by_path( 'partners', OBJECT, 'post' ); ?>
<?php $images_gallery = get_post_gallery_images( $section_content->ID ); //var_dump( $images_gallery ); ?>

      <div class="swiper-container post-slider" style="height:300px;">
        <div class="swiper-wrapper">
          <?php if( $images_gallery ): foreach( $images_gallery as $image ): ?>
          <?php  //get the id of the image post.
             $image_id = attachment_url_to_postid( $image );
             var_dump( $image_id );
             //get the image "post" information
             $attached_image = get_post( $image_id );
             //get the image title
             $image_title = $attached_image->post_title;
             //get the image caption
             $image_caption = $attached_image->post_excerpt;
             //var_dump($image_id, $attached_image);
            ?>
            <div class="swiper-slide" style="background-image:url('<?php echo $image; ?>');background-size:cover;">
              <!-- <img class="img-fluid w-100" src="" > -->
              <small class="text-white m-4 d-none d-md-block" style="position:absolute;top:0;"><?php echo $image_title; ?></small>
            </div>
          <?php endforeach; ?>
          <?php endif; ?>
        </div>
        <div class="swiper-scrollbar"></div>
      </div>

After searching here, I've found a quick solution to obtain the gallery images info like captions, description and title, but I have a problem with the title and the id. Some images id are not returned and if I use var_dump() I can see that the id value returned from the function attachment_url_to_postid() is int(0).

I discovered that wordpress will load an image that at the end of the url has the dimension appended. I've checked the images permalink in the media library but the url is ok and didn't has the dimension appended like the one that is used to display the file: http://myasite/wp-content/uploads/2020/03/my-image-1024x683.jpg. As I can understand to get the id is needed an url like this: http://myasite/wp-content/uploads/2020/03/my-image.jpg.
Is there a fix?

Share Improve this question edited Mar 19, 2020 at 13:16 sialfa asked Mar 19, 2020 at 10:32 sialfasialfa 32910 silver badges29 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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 [gallery ids="1,2,3" size="large" /] 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/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. :)

发布评论

评论列表(0)

  1. 暂无评论