I successfully implemented Masonry-Gallery for WordPress website by using Bootstrap 5 combined with “Masonry-Layout”. It works great except in Safari where the layout often breaks, this is a known issue and the solution should be "imagesLoaded". I've tried a lot but I just can't add the required classes in the right place. As far as I know the code is as follows. Can anyone help?
<div class="row grid" id="gallery-masonry" data-masonry='{"percentPosition": true }'> <?php $images = get_field('gallery'); $size = 'large'; if( $images ): ?> <?php foreach( $images as $image_id ): ?> <div class="col-md-6 mb-3"> <div id="post-<?php the_ID(); ?>" class="card rounded-0" > <?php echo wp_get_attachment_image( $image_id, $size, "", ["class" => "card-img-top rounded-0","alt"=>""]); ?> </div> </div> <?php endforeach; ?> <?php endif; ?> </div> <!--Masonry-Layout--> <script src="https://cdn.jsdelivr.net/npm/masonry-layout@4.2.2/dist/masonry.pkgd.min.js" integrity="sha384-GNFwBvfVxBkLMJpYMOABq3c+d3KnQxudP/mGPkzpZSTYykLBNsZEnG2D9G/X/+7D" crossorigin="anonymous" async></script> <!--ImagesLoaded--> <script src="https://unpkg.com/imagesloaded@5/imagesloaded.pkgd.min.js"></script> <!--Script ImagesLoaded--> <script> var $grid = $('.grid').imagesLoaded( function() { // init Masonry after all images have loaded $grid.masonry({ // options... }); }); </script>
P粉2264132562024-01-29 16:20:18
Check out my post on adding Masonry to your WordPress site.
https://www.customfitwebdesign.com/how to add masonry layout in wordpress/
Note: Masonry is actually built into the core of WordPress ( wp-includes/js/masonry.min.js ). This WordPress version of Masonry also includes ImagesLoaded (imagesloaded.min.js).