0
votes

I'm building a custom gallery slider for a Wordpress theme and I have an architecture problem:

While I'm in the post template (single.php) I can easily retrieve an array with all post's galleries images via this code

$galleries = get_post_galleries( $post, false); 

(BTW: False parameter is to have theirs url instead of the images themselves)

but when I click on a specific gallery's image, and I'm redirected to the attachment template (attachment.php), then it's impossible to have that same array.

I tried with:

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

but this doesn't work properly. Indeed if I build a gallery with some pictures which were originally attached to another post (a older one, for example), the post_parent parameter will refer to that old post, instead of the one which redirected me to the attachment template.

Well, this is a problem because my slider script is loaded in the attachement.php and it can't handle the right array of pictures.

I can't trigger it while in single.php because the slideshow start after clicking on a gallery image.

(For the moment I discard the idea of making a more complex script that avoid the loading of attachment.php tempalte).

I'm looking for a workaround to retrive in PHP the right array while in attachment template.

1
wordpress.stackexchange.com/questions/80408/… Check this post it might be the same thing you're after. - nCore
already read. Not the same problem. Thank you anyway. - user3154898
I think it's simply impossible. This post exaplins better than me the issue (even if it mainly deals with another problem): wptavern.com/the-problem-with-image-attachments-in-wordpress - user3154898

1 Answers

0
votes

I managed to accomplish that in this way, inside the loop, in 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( );

Personal note: I think the bug resides in the chain of calls:

get_post_galleries, do_shortcode_tag, gallery_shortcode

not transmitting the post ID parameter correctly so that at a certain point, the attachment ID is used instead of the one supplied by the user in the first get_post_galleries call.