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

wp_get_attachment_url images in wrong order

programmeradmin2浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

I have this bit of code in my xml feed and it's picking up the images in a different order than it is in the WordPress post.

.png

Here in the admin area I can drag the images around, and the page will display the top left image first and the bottom right image last. I would like my xml feed to give the images the same order. Thanks!

$attimages = get_attached_media('image', $post->ID);
                          $counter = 1;
                          foreach ($attimages as $image) {
                            if ($counter <= 50) {
                              echo '<image id="'.$counter.'"><url>'.wp_get_attachment_url($image->ID).'</url></image>' . "\r\n";
                            }
                            $counter++;
                          }
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

I have this bit of code in my xml feed and it's picking up the images in a different order than it is in the WordPress post.

https://i.sstatic/E89eg.png

Here in the admin area I can drag the images around, and the page will display the top left image first and the bottom right image last. I would like my xml feed to give the images the same order. Thanks!

$attimages = get_attached_media('image', $post->ID);
                          $counter = 1;
                          foreach ($attimages as $image) {
                            if ($counter <= 50) {
                              echo '<image id="'.$counter.'"><url>'.wp_get_attachment_url($image->ID).'</url></image>' . "\r\n";
                            }
                            $counter++;
                          }
Share Improve this question asked Mar 31, 2020 at 9:13 AionPhireflyAionPhirefly 1
Add a comment  | 

1 Answer 1

Reset to default 1

Gallerys aren't implemented the way you're pulling in images, the images in a gallery might not be attached to the post at all.

get_attached_media will give you the images attached to the post by the order of their ID.

This means:

  • Images you added to the post, then deleted will be in this array
  • Images in galleries attached to other posts won't be in this array

If we think it through logically, if your post had 2 galleries, each with the same photos but ordered differently, how would that work? Which order is correct? Neither. This function has nothing to do with Galleries

So What Are Galleries Then?

Shortcodes/Blocks.

E.g. shortcode:

[gallery ids="1,2,5,4"]

Or block:

<!-- wp:gallery {"ids":[36,30,22,15,14]} -->
<figure class="wp-block-gallery columns-3 is-cropped"><ul class="blocks-gallery-grid"><li class="blocks-gallery-item"><figure><img src="http://one.wordpress.test/wp-content/uploads/2020/03/be.png" alt="" data-id="36" data-full-url="http://one.wordpress.test/wp-content/uploads/2020/03/be.png" data-link="http://one.wordpress.test/be/" class="wp-image-36"/></figure></li>...etc...</ul></figure>
<!-- /wp:gallery -->

The order of the IDs in the attributes is what determines the order. Using get_attached_media to try to recreate a gallery in content is a dead end that will not work. You need to look at the shortcode/block instead

发布评论

评论列表(0)

  1. 暂无评论