• We have built a custom gallery that uses the gallery block but our client recently pointed us that these galleries would not display anymore.
    A quick check shows us that we are not able to access the ids of the gallery’s images anymore.

    Here is how we coded things on our end:

    function get_img_gallery( $post ) {
    	if ( has_block( 'gallery', $post->post_content ) ) {
    		$els = parse_blocks( $post->post_content );
    		foreach ( $els as $el ) {
                if ( $el['blockName'] == 'core/gallery' ) :
    				$gallery_imgs_ids = $el['attrs']['ids'];
    				return $gallery_imgs_ids;
    			endif;
    		}
    	}
    }

    We are getting an error “Undefined index: ids” so it seems that “ids” are no longer passed in the “attrs” array? (You can check that on the provided link, right before the Google Map integration near the footer of the page)

    Might it be that we missed an update regarding how these attributes work? How can we fix that?

    Thanks!

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator threadi

    (@threadi)

    Unfortunately you didn’t write which WordPress versions you are referring to exactly. My check just now revealed a difference in the gallery block between 5.8.3 and 5.9.1. This was due to the fact that the Gutenberg team revised the block last year. This is documented here: https://github.com/WordPress/gutenberg/projects/56

    The biggest difference is that the gallery images are now stored as separate blocks and no longer as list items. This also gives more possibilities in the design of the frontend.

    If I see it correctly, you need the IDs of the images in the gallery for your customisation. You can determine these by reading out the individual image blocks via $el[‘innerBlocks’] and fetching the field “id” from them. Rough, untested example:

    $gallery_imgs_ids = [];
    foreach( $el['innerBlocks'] as $subel ) {
     $gallery_imgs_ids[] = $subel['attrs']['id'];
    }
    return $gallery_imgs_ids;

    For more questions about changes to the Gutenberg block, I would ask you to contact: https://github.com/WordPress/gutenberg/issues

    Thread Starter Julien Vandermeersch

    (@jvkonfi)

    I did indeed forget to list the wordpress version used, sorry about that.

    We’re using v5.9.1 and that is exactly what I ended up doing, and it seems to be doing the trick.

    Thanks a lot for your answer!

    Moderator threadi

    (@threadi)

    Great, if I could answer your question. You are welcome to set the topic to solved.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Issues getting image ids from Gutenberg gallery block’ is closed to new replies.