【问题标题】:Wordpress: retrive an array with all post galleries images inside attachment.phpWordpress:检索一个数组,其中包含附件.php 中的所有帖子画廊图像
【发布时间】:2014-09-30 15:08:11
【问题描述】:

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

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

$galleries = get_post_galleries( $post, false); 

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

但是当我点击特定画廊的图片时,我被重定向到附件模板 (attachment.php),那么不可能有相同的数组。

我试过了:

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

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

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

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

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

我正在寻找一种解决方法,以便在 PHP 中检索附件模板中的正确数组。

【问题讨论】:

标签: php wordpress wordpress-theming image-gallery


【解决方案1】:

我设法以这种方式在循环内的附件.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

未正确传输帖子 ID 参数,因此在某个时刻,附件 ID 被使用,而不是用户在第一次 get_post_galleries 调用中提供的那个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-13
    • 2020-05-05
    • 2013-09-21
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多