I'm using gutenberg gallery block inside a post and I'm trying to create a button which contains all of the image ids in the gallery block as html data attributes such that later when I output the content to the page I can have access to those ids using javascript
. Basically I'm trying to create a lightbox feature for a custom post type.
The problem is that I can't get access to the gutenberg gallery block data.
Here's my code
while ($custom_post_type->have_posts()) {
$custom_post_type->the_post();
$gallery = get_post_gallery(get_the_id(), false);
$ids = explode(",", $gallery['ids']);
}
And here's that button with html data attributes
<button class="gallery"
<?php
for ($i = 0; $i < count($ids); $i++) {
$img_link = wp_get_attachment_image_url($ids[$i], 'full');
echo "data-img-" . $i . " = " . $img_link . " ";
}?>
>
Light-box
</button>
But it does not work, $ids
is empty. It prints out this
<button class="gallery">Light-box</button>
Thanks for your help!
Edit
I'm using wordpress blocks on the post page, I'm not quite certain how they have been generated, but they work out of the box.