PHP使用引导循环自定义字段

PHP使用引导循环自定义字段,php,html,wordpress,twitter-bootstrap,advanced-custom-fields,Php,Html,Wordpress,Twitter Bootstrap,Advanced Custom Fields,所以我有这个代码,我想重复一下。使用wordpress acf生成图像: `<div class="row"> <?php if( get_field('image_1') ): ?> <img class="this-image" src="<?php the_field('image_1'); ?>" /> <?php endif; ?> </div>` ` " /> ` 我只是想知道

所以我有这个代码,我想重复一下。使用wordpress acf生成图像:

`<div class="row">
    <?php if( get_field('image_1') ): ?>
        <img class="this-image" src="<?php the_field('image_1'); ?>" />
    <?php endif; ?>
</div>`
`
" />
`
我只是想知道如何循环这个,我还需要增加数字(即,image_1,image_2,image_3)


我很难理解PHP的逻辑,如果已经有类似的帖子,请提供一个友好的链接,谢谢!

你可以试试这样的东西

`<div class="row">
    <?php 
    for (i=1; i<=3; i++) {
    if( get_field('image_'.i) ): ?>
        <img class="this-image" src="<?php the_field('image_'.i); ?>" />
    <?php endif; } ?>
</div>`
`
" />
`

您可以尝试类似的方法

`<div class="row">
    <?php 
    for (i=1; i<=3; i++) {
    if( get_field('image_'.i) ): ?>
        <img class="this-image" src="<?php the_field('image_'.i); ?>" />
    <?php endif; } ?>
</div>`
`
" />
`

使用下面的代码-它使用for循环来迭代从image_1到image_X的所有图像(其中X可以由您定义)。类似的代码可以通过添加一个数组来修改,以存储所有图像名称,以防没有图像名称的模式

<div class="row">
    <?php $numImages = 10; #Define how many images you have
     for ($i=1; $i<= $numImages; $i++){ #starting from image_1 ?>
    <?php if( get_field('image_' . $i)): ?>
          <img class="this-image" src="<?php the_field('image_' . $i); ?>" />
    <?php endif; ?>
    <?php } ?>
</div>

" />

使用下面的代码-它使用for循环遍历从image_1到image_X(其中X可以由您定义)的所有图像。可以通过添加一个数组来存储所有图像名称来修改类似的代码,以防图像名称没有模式

<div class="row">
    <?php $numImages = 10; #Define how many images you have
     for ($i=1; $i<= $numImages; $i++){ #starting from image_1 ?>
    <?php if( get_field('image_' . $i)): ?>
          <img class="this-image" src="<?php the_field('image_' . $i); ?>" />
    <?php endif; ?>
    <?php } ?>
</div>

" />

您可以使用
For循环
While循环
来完成此任务。您可以使用
For循环
While循环
来完成此任务。虽然我在理解这种情况下花括号是如何工作的方面有困难,但它只是帮助分离For循环和if语句吗?很简单for循环语法的开括号和关括号与
foreach($item的数组)相同:
endforeach;
Ah,我明白了。如果我想重复两次行,同时在其中保持相同的for循环,怎么样?所以你把循环放在一个函数中,比如说
foo()
然后每次需要使用循环时只需调用它。例如
foo();
foo()
这仍然会按顺序递增吗?它不会每次调用函数时都重新启动?这非常有效!虽然我很难理解花括号在这种情况下是如何工作的?它只是帮助分离for循环和if语句吗?它只是for循环语法的简单开括号和闭括号,与
foreach($array as$item):
endforeach;
啊,我明白了。如果我想重复两次该行,同时在内部保持相同的for循环,怎么样?因此您将循环放入一个函数中,比如说
foo()
,然后每次需要使用循环时就调用它。例如
foo()
foo();
这仍然会按顺序递增吗?它不会在每次调用函数时重新启动吗?