questions

How do I get Photoswipe caption text?

Rate this post

I’m hoping this isn’t too silly of a question, but I’ve been stuck on this problem for hours now and could use some help/guidance with it.

I’m using ACF Gallery in WP, and I’m now trying to turn this into a popup slider. So I decided to use the Photoswipe plugin to achieve this.

I have the slider working fine, it’s just that I can’t figure out how to output the image caption with each image.

<?php
$popup_gallery = get_field('popup_gallery','option');
$popup_gallery_json = json_encode($popup_gallery,JSON_FORCE_OBJECT);
?>

<div class="popup-gallery-data" data-json='<?php echo $popup_gallery_json;?>'
    data-json-length="<?php echo count($popup_gallery);?>" >
</div>
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="pswp__bg"></div>
    <div class="pswp__scroll-wrap">
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>
var galleryArr = [];
    var galleryJson = $('.popup-gallery-data').data('json');
    // get the number of objects
    var jsonLength = $('.popup-gallery-data').data('json-length');


    // push each object into the array;
    for (let i = 0; i < jsonLength; i++) {
        galleryArr.push(galleryJson[i]);

    }

    $('a[href*="#popup-gallery"]').click(function (e) {
        e.preventDefault();

        // Prevent errors.
        if (jsonLength > 0) {
            popUpGallery(galleryArr);
        }

    })


var popUpGallery = function (data) {

    const json = data;
    var pswpElement = document.querySelectorAll('.pswp')[0];

    // build items array
    var items = [];

    json.map((img) => {
        items.push({
            src: img.url,
            w: img.width,
            h: img.height
        })
    })


    // define options (if needed)
    var options = {

    };

    var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
    gallery.init();

}

 

✔️Solution:

caption should be an available key so you can update your code like below:

 json.map((img) => {
        items.push({
            src: img.url,
            w: img.width,
            h: img.height,
            title: img.caption
        })
    })

Leave a Reply

Your email address will not be published.

Back to top button