Is there a Magnific Popup function for getting images from the CMS under a certain category?

24 Views Asked by At

So I have a Wordpress website with a gallery of images using Advanced Custom Fields (ACF) using a repeater field with an image ID field and checkbox field for categories (see image of ACF setup in the back end below).

enter image description here

I then have a page called gallery, that simply lists all the categories.

Now, what I would like is, when a category is clicked, show a lightbox using Magnific Popup and pull in all the images that are under that specific category in the ACF gallery in the back end.

Can this be done?

Thanks in advance!

Here is my current code below:

<section class="container">
                    <div id="image-gallery" class="grid col-3">
                        <?php
                        if( have_rows('gallery_categories') ):
                            while ( have_rows('gallery_categories') ) : the_row();              
                                ?>
                                <a class="cd-work-link image-gallery-link" data-category="<?php the_sub_field('gallery_category_value'); ?>" href="#">
                                    <div class="work-thumb" style="background-image: url('<?php echo wp_get_attachment_image_src( get_sub_field('gallery_category_image'), "large" )[0]; ?>')">
                                        <div class="overlay">
                                            <div class="colour"></div>
                                            <i class="fa-solid fa-magnifying-glass caption"></i>
                                        </div>
                                    </div>
                                    <h2><?php the_sub_field('gallery_category_name'); ?></h2>
                                </a>
                                <?php
                            endwhile;
                        endif;
                        ?>
                    </div>
                </section>

                <div id="gallery-images" class="hidden">
                    <?php
                    if( have_rows('image_gallery') ):
                        while ( have_rows('image_gallery') ) : the_row();              
                            ?>
                            <a class="gallery-image" data-categories="<?php the_sub_field('gallery_image_categories'); ?>" href="<?php echo wp_get_attachment_url( get_sub_field('gallery_image_url') ); ?>"></a>
                            <?php
                        endwhile;
                    endif;
                    ?>
                </div>

                <script>
                    jQuery(document).ready(function($) {
                        $('.image-gallery-link').on('click', function(e) {
                            e.preventDefault();
                            var category = $(this).data('category');
                            var images = $('.gallery-image[data-categories*="' + category + '"]');

                            if (images.length > 0) {
                                var items = [];
                                images.each(function() {
                                    var imageUrl = $(this).attr('href');
                                    items.push(imageUrl);
                                    console.log("Added image to items array: " + imageUrl);
                                });

                                console.log("Items array:", items);

                                $.magnificPopup.open({
                                    items: items,
                                    gallery: {
                                        enabled: true
                                    },
                                    type: 'image'
                                });
                            } else {
                                alert('No images found for this category');
                            }
                        });
                    });
                </script>
0

There are 0 best solutions below