【问题标题】:ACF gallery show first image and open lightbox on clickACF 画廊显示第一张图片并在点击时打开灯箱
【发布时间】:2021-01-08 07:54:48
【问题描述】:

我正在尝试创建一个存档页面来展示一些实现,它们都有一个包含多个图像的画廊。我使用 ACF 创建图库,使用 Simple Lightbox 插件创建灯箱。我找到了一个关于如何组合两个插件的示例,它与我需要的很接近,但我自己无法弄清楚其余部分。

现在画廊中的所有图片都显示出来了,我只需要显示第一张图片,当您点击图片时,我想在灯箱中打开图片并有可能以这种方式浏览画廊。

我目前所拥有的:

<?php if ( have_posts() ) {
        while ( have_posts() ) { the_post(); ?>
            <article id="realisatie-<?php the_ID(); ?>" <?php post_class(); ?> style="background-image: url(<?php echo $images[0] ?>);">
                <?php
                $images = get_field('realisatie_beelden');
                $image_1 = $images[0];
                if( $images ) { ?>
                <div class="realisatie__gallery" >
                    <?php foreach( $images as $image ) {
                        $content = '<a class="gallery_image" href="'. $image .'">';
                        $content .= '<img src="'. $image .'" alt="'. $image .'" />';
                        $content .= '</a>';
                        
                        if ( function_exists('slb_activate') ) {
                            $content = slb_activate($content);
                        }
                        ?>
                    <?php } ?>
                </div>
                <?php } ?>
            </article>
        <?php }
    } ?>

【问题讨论】:

  • 只为除第一张图片之外的所有内容输出空链接。然后应该提供灯箱功能,而不需要它们实际显示任何内容。
  • $images = get_field('realisatie_beelden',$post_id);在这里传递帖子ID,以便您可以获取字段值并尝试

标签: php wordpress gallery advanced-custom-fields lightbox


【解决方案1】:

感谢评论,我找到了适合我的解决方案。 我只显示第一个元素的图像,其他链接为空。

<?php if ( have_posts() ) {
        while ( have_posts() ) { the_post(); ?>
            <article id="realisatie-<?php the_ID(); ?>" <?php post_class(); ?>>
                <?php
                $images = get_field('realisatie_beelden');
                $image_1 = $images[0];
                if( $images ) { ?>
                    <?php
                    $i = 0;
                    
                    foreach( $images as $image ) {
                        if ($i >= 1) {
                            $content = '<a href="'. $image .'"></a>';
                        } else {
                            $content = '<a href="'. $image .'">';
                            $content .= '<img src="'. $image .'" />';
                            $content .= '</a>';

                            $i++;
                        }
                        
                        if ( function_exists('slb_activate') ) {
                            $content = slb_activate($content);
                        }

                        echo $content;
                        ?>
                    <?php } ?>
                <?php } ?>
            </article>
        <?php }
    } ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    • 2017-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多