• Resolved mb0wordpress

    (@mb0wordpress)


    In version 5.1.6 of WordPress get_post_gallery_images() function works perfectly when used in my theme. When I updated to the 5.4.2 version this function might have a bug? It returns false (indicating there are no gallery blocks in the post).

    I checked and made sure I was passing in the correct post id (I was) and that the post I was passing in actually had gallery blocks(it does).

    Can someone help me find a way around this? I need to get access to the urls of the images in the first gallery of a post. You can see why this function was perfect for me.

    I wasn’t sure if I should create a bug report. I tried to find anything similar about this but didn’t. Im very new to WordPress forums, not so new to theme development though.

Viewing 3 replies - 1 through 3 (of 3 total)
  • you can use shomething like this:

    
    <?php
    $gallery = $product->get_gallery_image_ids();
    ?>
    
    <div>
    
        <?php
        foreach ( $gallery as $imgSource ) {
    
            echo wp_get_attachment_image( $imgSource );
    
    	}
        ?>
    
    </div>
    

    for more info and see accepted parameters:
    wp_get_attachment_image

    Moderator bcworkz

    (@bcworkz)

    get_post_gallery_images() is specifically for getting galleries which are defined with the [gallery] shortcode (old style gallery in “classic” editor). The block editor galleries do not use this shortcode, so the function is ineffective for new style galleries.

    I’m not sure if there is an equivalent function for new style galleries. You can get new style gallery attachment IDs by using preg_match() on post content. Use a regexp that will capture the string of IDs in the block’s comment header. Explode the string into an array, then foreach the array and use wp_get_attachment_image() like Nik illustrated. The $product->get_gallery_image_ids() part is meant for product galleries, which it’s not clear if you are using or not. What I’ve described is for block editor galleries.

    Thread Starter mb0wordpress

    (@mb0wordpress)

    I knew what bcworkz said about it not working with the block editor galleries (I’ve had problems with this function before), so now Im just confused as to why it worked when I used it in v 5.1.6.

    Anyway, with a combination of the two answers I threw together this which seems to work fine. The theme I’m developing is for myself, so I know I can always guarantee that the first gallery will the be one I need (and I know it will always be there), which is why I was fine hardcoding it.

    $postBlocks = parse_blocks($post->post_content);
    $firstGalleryIds = $postBlocks[0]["attrs"]["ids"];
    $previewImages = [];
    
    foreach ( $firstGalleryIds as $imgSource ) {
    
    	$previewImages[] = wp_get_attachment_image_src( $imgSource, $size = 'large' )[0];
    
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘get_post_gallery_images()’ is closed to new replies.