如何获取 Photoswipe 标题文本?

How do I get Photoswipe caption text?

我希望这不是一个太愚蠢的问题,但我已经在这个问题上停留了几个小时,并且可以使用一些 help/guidance 来解决它。

我在 WP 中使用 ACF Gallery,现在我正试图将其变成弹出式滑块。所以我决定使用 Photoswipe 插件来实现这一点。

我的滑块工作正常,只是我不知道如何为每张图片输出图片标题。

<?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();

}

caption 应该是一个可用的密钥,因此您可以像下面这样更新您的代码:

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