Last active
January 24, 2022 03:34
-
-
Save erikyo/71472e31de794e12e0f892f88954aff0 to your computer and use it in GitHub Desktop.
fancybox 3 integration into wordpress Gutenberg gallery block
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ($('.wp-block-gallery')) { | |
$('.blocks-gallery-item').click(function() { | |
var galleryImages = $(this).closest('.wp-block-gallery').find('a'); | |
var gallery = []; | |
galleryImages.each(function( index, galleryItem ) { | |
var caption = $(this).parent().find('figcaption') ? $(this).find('img').attr('alt') : $(this).parent().find('figcaption') ; | |
gallery.push({ | |
src : galleryItem.href, | |
opts : { | |
caption: caption | |
} | |
}) | |
}); | |
$.fancybox.open( gallery, { loop: false , index: $(this).closest('li').index() } ); | |
return false; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2021/08/22 - Updated to wp 5.8
To get it working you need to create a WordPress gallery and in the Gallery settings tab, under "Link to:" select "Media File".
You need to load the fancybox script (http://fancybox.net/)