Php ACF计数库图像

Php ACF计数库图像,php,wordpress,advanced-custom-fields,acfpro,Php,Wordpress,Advanced Custom Fields,Acfpro,我正在尝试使用ACF gallery字段制作不同大小图像的网格 我以前曾在repeater字段中通过计算行数来完成此操作,但无法调整此操作以使用gallery字段 我的目标是使用两种不同的图像大小创建一个由10幅图像组成的网格 图像1、2、3、6、7、8将是一个尺寸 图像4、5、9、10的大小会有所不同 我当前的标记是: <?php $images = get_field('home-image-grid'); $size = 'full'; if( $ima

我正在尝试使用ACF gallery字段制作不同大小图像的网格

我以前曾在repeater字段中通过计算行数来完成此操作,但无法调整此操作以使用gallery字段

我的目标是使用两种不同的图像大小创建一个由10幅图像组成的网格

  • 图像1、2、3、6、7、8将是一个尺寸
  • 图像4、5、9、10的大小会有所不同
我当前的标记是:

<?php 
    $images = get_field('home-image-grid');
    $size = 'full';
    if( $images ): 
?>
    <ul>
        <?php foreach( $images as $image ): ?>
            <li>
                <?php echo wp_get_attachment_image( $image['ID'], $size ); ?>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

我已经尝试了我以前使用的标记,但它只输出图像1

<?php 
    $i = 1;
    $images = get_field('home-image-grid');
    $size = 'full';
    if( $images ): 
?>

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

        <?php if ( $i == 1 ) { ?>
            image 1
        <?php } elseif ( $i == 2 ) { ?>
            image 2
        <?php } elseif ( $i == 3 ) { ?>
            image 3
        <?php } elseif ( $i == 4 ) { ?>
            image 4
        <?php } ?>

    <?php endforeach; ?>

<?php endif; ?>

图1
图2
图3
图4

我想这是因为foreach的陈述。我如何才能让它工作?

这项工作将在gallery或repeater字段中完成,两者都返回阵列

您只是忘记在循环中添加迭代
$i++

所以你的循环会是这样的

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

    <?php if ( $i == 1 ) { ?>
        image 1
    <?php } elseif ( $i == 2 ) { ?>
        image 2
    <?php } elseif ( $i == 3 ) { ?>
        image 3
    <?php } elseif ( $i == 4 ) { ?>
        image 4
    <?php } 
     $i++;
     endforeach; ?>

图1
图2
图3
图4
另外,如果您的数组索引没有任何复杂的条件,那么您可以将foreach循环与数组索引一起使用,如

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

    <?php if ( $index == 0 ) { ?>
        image 1
    <?php } elseif ( $index == 1 ) { ?>
        image 2
    <?php } elseif ( $index == 2 ) { ?>
        image 3
    <?php } elseif ( $index == 3 ) { ?>
        image 4
    <?php } 
     endforeach; ?>

图1
图2
图3
图4

如果两个独立的背景图像:


我不确定这是否是您发布的完整代码,但您肯定应该在endforeach之前使用$I++递增$I?目前,在您的循环中,$i始终等于1。