首页 文章

Wordpress:在attachment.php中检索包含所有帖子库图像的数组

提问于
浏览
0

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

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

$galleries = get_post_galleries( $post, false);

(顺便说一句:假参数是他们的网址而不是图像本身)

但是当我点击特定的图库's image, and I' m重定向到附件模板( attachment.php )时,就不可能拥有相同的数组 .

我尝试过:

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

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

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

我无法在 single.php 中触发它,因为幻灯片显示在单击图库图像后开始 .

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

我正在寻找一种解决方法,在附件模板中用PHP重新检索正确的数组 .

1 回答

  • 0

    我设法以这种方式,在循环内,在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( );
    

    个人注意:我认为该bug存在于调用链中:

    get_post_galleries,do_shortcode_tag,gallery_shortcode

    不正确地传输帖子ID参数,以便在某一点使用附件ID而不是用户在第一次get_post_galleries调用中提供的附件ID .

相关问题