Wordpress:在attachment.php中检索一个包含所有文章库图像的数组


Wordpress: retrive an array with all post galleries images inside attachment.php

我正在为Wordpress主题构建一个自定义库滑块,但我遇到了一个架构问题:

当我在帖子模板(single.php)中时,我可以通过这个代码轻松地检索到一个包含所有帖子库图像的数组

$galleries = get_post_galleries( $post, false); 

(顺便说一句:错误的参数是有他们的url而不是图像本身)

但是,当我点击特定库的图像,并重定向到附件模板(attachment.php)时,就不可能有相同的数组。

我尝试过:

$galleries = get_post_galleries( $post->post_parent, false);

但这不能正常工作。事实上,如果我用一些最初附加到另一篇帖子(例如,一个旧帖子)的图片构建了一个库,post_parent参数将引用那个旧帖子,而不是将我重定向到附件模板的那个。

这是一个问题,因为我的滑块脚本加载在attachement.php中,它无法处理正确的图片数组。

我在single.php中无法触发它,因为幻灯片放映是在单击图库图像后开始的。

(目前,我放弃了制作更复杂的脚本以避免加载attachment.php tempalte的想法)。

我正在寻找一种变通方法,在PHP中检索附件模板中的正确数组。

我通过这种方式,在循环内部,在attachment.php中完成了这一点:

// switch to the parent post, the one holding the [gallery] shortcode(s)
$post = get_post( wp_get_post_parent_id( get_the_ID( ) ), OBJECT );
setup_postdata( $post );
// get the galleries
$galleries = get_post_galleries( $post, false );
// VERY IMPORTANT: restore the original post (the attachment)
wp_reset_postdata( );

个人注意:我认为错误存在于调用链中:

get_post_galleries,do_shortcode_tag,gallery_shortcode

没有正确地发送post ID参数,从而在某个时刻,使用附件ID而不是用户在第一个get_post_galleries调用中提供的附件ID。