• Resolved James Revillini

    (@jrevillini)


    I’m just contributing code that works (for me) with the native Gallery block and this plugin:

    document.addEventListener("DOMContentLoaded", function() {
        // Select all divs with an ID starting with "gallery-"
        document.querySelectorAll('.wp-block-gallery').forEach(function(gallery) {
            // For each gallery item within this gallery div
            gallery.querySelectorAll('.wp-block-image').forEach(function(item) {
                // Retrieve the text inside the figcaption element (if exists)
                const caption = item.querySelector('figcaption');
                const captext = caption ? caption.textContent : '';
                // Add the data-sub-html attribute to the corresponding <a> element
                const link = item.querySelector('a');
                if (link) {
                    link.setAttribute('data-sub-html', captext);
                }
            });
    
            // Use className property of <figure> gallery element as ID because it is unique
            const id = gallery.className;
    
            // Do something with each selected div
            lightGallery(gallery, {
                plugins: [lgFullscreen, lgMediumZoom, lgVideo, lgZoom, lgPager],
                selector: 'a',
                galleryId: id,
            });
        });
    });
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[contribution] init script to support Gallery Block’ is closed to new replies.