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?
- Convert multiple image blocks to gallery block in Gutenberg?
- get_post_gallery with Gutenberg
- How do I delete all UNUSED images from my uploads directory?
- Prevent WordPress from adding image’ title automatically
- Overide Gallery Default Link to Settings
- get_post_gallery_images returns thumbs . I want full size
- Navigate with keyboard in Gallery shortcode
- get images from post and make a zip
- Bulk edit wordpress images alt and title attributes
- Making WordPress Gallery (.gallery-item) Responsive?
- How do you modify the HTML output of a Gallery item (using the gallery shortcode)?
- 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 can I make all gallery images to open in a new window?
- Displaying a featured image (only img url) as the img src?
- 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?
- Editing built in Gallery shortcode to filter by categories
- Gallery images stored elsewhere (preferably Google Drive)
- WordPress gallery previous/next buttons showing all media library images
- Any way to “combine” galleries or show multiple galleries as one?
- How to remove buttons from gutenberg toolbar
- Gutenberg editor get post featured image by id
- Best option for photo/gallery handling?
- Get meta data from image
- Bulk edit Gutenberg single images to change from linking to media file to attachment page
- WooCommerce-like featured gallery
- Override img class in gallery
- Pull Random Attachments With Pagination
- Extracting gallery images in WordPress 3.5 on index.php
- How to use photo in multiple posts?
- 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
- Retrieve an array of image URLs that belong to a gallery
- Add width & height attributes to Gutenberg’s image output?
- update_post_meta attachment_image_alt description for Gutenberg
- Ho to add “Create Slider” option to default “Add Media” popup?
- How to make a gallery with dynamic images from URLs?
- How can I display an image description, from an image gallery?
- Removing featured image from gallery?
- Don’t show images inserted into content
- Gallery with thumbnail carousel
- Ways to show multiple, separate galleries per post?
- Native gallery sorting is ignored
- Using Jetpack carousel w/o creating a gallery [closed]
- Media gallery images url instead of ID on WP API Response
- Get all image IDs from the Media Library
- Allow users to contribute images to a post
- How to upload and style svg logo?
- Weird failing request
- How to remove the images from “Uploaded to this post”?
- limit number of images shown from gallery
- How to automatically populate a gallery with images attached to posts of one category?
- What is the plugin to import image from URL?
- Excluding post thumbnail from the attachment.php when using wordpress gallery
- photo gallery implementation like tmz
- 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?
- How to enlarge thumbnail on mouseover?
- attach several images to post + gallery
- Place image caption outside of figure tag (Gutenburg Image Markup)
- Change Image Size option in the settings in bulk
- How to join Caption AND Description Meta Fields as one combined-caption in Image Block
- Using srcsets in a custom block
- Allow users to upload and display multiple photos
- Implementing a modal(lightbox) for all images in all post-gallery posts
- how automatically show the image title before the image caption/description in a wordpress gallery?
- change the h3 subtitles of the wordpress gallery shortcode
- A query to substitute images paths in gutenberg (website migration)?
- 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
- attachment.php & flexslider—linking thumbnail to specific image
- Thumbnail gallery by date and taxonomy
- Breadcrumbs for Single Image page
- Thickbox not displaying just the image, instead showing full page in lightbox
- Multiple gallery thumnbnails on one page
- Slider do not add alt attribute. Cant get alt attribute from a media library!
- wp_get_attachment_link() add rel attribute if the link points to direct image
- Paginate Gallery [closed]
- How do I set the sizes of my thumbnails when inserting a [gallery]?
- *box for images displayed as a post’s [gallery] [duplicate]
- Display X images from a gallery in the sidebar
- Getting top Image From the Gallery and printing out a thumbnail with Exact Dimensions
- 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