ACF - 从自定义 Post 类型获取图库

ACF - Getting a gallery from a Custom Post Type

我正在尝试显示自定义 post 类型的图库。

我使用自定义 Post 类型 UI 插件创建了一个新的自定义 Post 类型,并将其命名为 'photo'。

我的 ACF 自定义字段名为 'gallery'。

这是一个单页网站,所以我试图显示来自 index.php 的图像。

我从以下代码开始,当图库包含在常规 Post 页面中时,代码运行良好:

<?php

$images = get_field('gallery');

if( $images ): ?>

        <?php foreach( $images as $image ): ?>

                     <div class="item" ><img src="<?php echo $image['sizes']['large']; ?>"  /></div>

        <?php endforeach; ?>

<?php endif; ?>

我现在正尝试从自定义 post 类型中提取图像,如下所示:

<?php

   $loop = new WP_Query( array( 'post_type' => 'photo') );
     while ( $loop->have_posts() ) : $loop->the_post();

$images = get_field('gallery');

if( $images ): ?>

        <?php foreach( $images as $image ): ?>

                         <div class="item" ><img src="<?php echo $image['sizes']['large']; ?>"  /></div>

            <?php endforeach; ?>

    <?php endif; ?>

但这是行不通的——事实上,整个页面都变成空白,就像语法错误一样。

有什么解决办法吗?

非常感谢!

您错过了 endwhile - 猜猜这就是您的页面变为空白的原因。