【问题标题】:How do I get Photoswipe caption text?如何获取 Photoswipe 标题文本?
【发布时间】:2021-01-29 11:36:41
【问题描述】:

我希望这不是一个太愚蠢的问题,但我已经被这个问题困扰了好几个小时,并且可以使用一些帮助/指导。

我在 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();

}

【问题讨论】:

    标签: javascript wordpress advanced-custom-fields photoswipe


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的回复。我如何将该标题输出到 PSWP 滑块中?
    • 看起来它使用了title 键...更新了我的答案。
    • 哇,改变就这么简单.....感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多