Using birgire’s suggestions, I came up with the following solution that is working well with both the old pre-WP 4.x data and the newer posts that have been added under 5.0.
// if there is a gallery block do this
if (has_block('gallery', $post->post_content)) {
$post_blocks = parse_blocks($post->post_content);
$ids = $post_blocks[0][attrs][ids];
}
// if there is not a gallery block do this
else {
// gets the gallery info
$gallery = get_post_gallery( $post->ID, false );
// makes an array of image ids
$ids = explode ( ",", $gallery['ids'] );
}
Related Posts:
- How to extend the gallery block in Gutenberg?
- get_post_gallery with Gutenberg
- How to include externally hosted images into a gallery block
- How to link Gutenberg Gallery images To Media file automatically?
- How to get the Gallery form/section just like in Gutenberg block?
- get_post_gallery with Gutenberg
- Convert multiple image blocks to gallery block in Gutenberg?
- How do I delete all UNUSED images from my uploads directory?
- Set default image link target in Gutenberg image block
- WordPress adding scaled images that don’t exist (1536×1536 and 2048×2048)
- Prevent WordPress from adding image’ title automatically
- Overide Gallery Default Link to Settings
- How to have different captions for same image, for galleries?
- get_post_gallery_images returns thumbs . I want full size
- WP 3.5 and Galleries – how to count images?
- Cropped featured image replaces original image in gallery
- get images from post and make a zip
- Bulk edit wordpress images alt and title attributes
- Change the output for [gallery] shortcode
- Making WordPress Gallery (.gallery-item) Responsive?
- Apply image width to tag
- Use Media Library to manage galleries like Nextgen (with folders, albums, collections, tags, categories, terms…)
- Setting up a gallery with images and a zip download
- How to remove title attribute from gallery links and images
- The WordPress Gallery, Grabbing The Link and Images?
- Displaying a featured image (only img url) as the img src?
- How to force WordPress to add new images at the beginning of the gallery?
- How to Make WordPress Default gallery responsive on mobile?
- get attributes/part of the gallery shortcode
- Different captions for the same image in multiple galleries. Edit Image in Gallery locally
- How to Add extra option to Image Block Settings?
- @wordpress/create-block image url in css
- Extend core block attributes in post content
- Gutenberg editor get post featured image by id
- hardcrop images in gutenberg “latest posts” block
- Best option for photo/gallery handling?
- Gallery post images on homepage?
- How to create a simple slideshow out of a post image gallery?
- How to change the target size of images clicked on in WordPress standard gallery
- Get meta data from image
- Bulk edit Gutenberg single images to change from linking to media file to attachment page
- Override img class in gallery
- Pull Random Attachments With Pagination
- wordpress get gallery images title
- Exclude images from “inserted into post” when trying to get attachments
- Is this post using WordPress Gallery?
- Turn off image captions in gallery view?
- Prevent Image Pasting in RichText Component
- Unyson Framework doesn’t show gallery images
- Multiple amp-image overlays. How to properly do this in wordpress?
- Update an image block style programatically
- Add width & height attributes to Gutenberg’s image output?
- update_post_meta attachment_image_alt description for Gutenberg
- Removing featured image from gallery?
- Creating a post image to show in a gallery
- Don’t show images inserted into content
- Gallery with thumbnail carousel
- Media gallery images url instead of ID on WP API Response
- Load Gallery Images with Title on Homepage BX Slider
- Get all image IDs from the Media Library
- Re-ordering images in existing post in wordpress 3.6.1
- Autosort uploads in galleries by filename
- How to display original unmodified thumbnail
- One picture belonging different galleries?
- Allow users to contribute images to a post
- Is there a wordpress filter to edit the markup of inline images?
- How to upload and style svg logo?
- Weird failing request
- How can I check if the first block is an image aligned left or right?
- How to automatically populate a gallery with images attached to posts of one category?
- Pagination between images (active/inactive)
- Excluding post thumbnail from the attachment.php when using wordpress gallery
- How do I display the featured image from an image gallery using a reference in code? (example within)
- Can i have 2 separate Galleries on 2 separate pages each go to a DIFFERENT Attachment.php file?
- attach several images to post + gallery
- How to disable a WP Image Block to link to itself?
- Using srcsets in a custom block
- Allow users to upload and display multiple photos
- how automatically show the image title before the image caption/description in a wordpress gallery?
- Images getting smashed
- change the h3 subtitles of the wordpress gallery shortcode
- How to re-arrange images in default WP Gallery more than one image at once?
- A query to substitute images paths in gutenberg (website migration)?
- Images 404 after wp migration to a subdomain
- How do i replace the title attribute of img tag with the image caption
- Show WordPress Image Title Under Custom Portfolio Image
- Replacing an Image gallery
- Thumbnail gallery by date and taxonomy
- How to display every pictures from all the posts on one page?
- How to add multiple images in a page
- Thickbox not displaying just the image, instead showing full page in lightbox
- Multiple gallery thumnbnails on one page
- How do I set the sizes of my thumbnails when inserting a [gallery]?
- Display X images from a gallery in the sidebar
- Custom filename when pasting an image from clipboard
- Adding different classes to different images depending on size for fine control of image layout in posts
- Automatically adding blog images to a portfolio/gallery
- Image Attachments Query Not Working If Post Is Loaded Via Ajax
- Gutenberg block HTML image rendering override
- Images uploaded in customized gallery do not link to media