使用short_open_tags将Foreach循环嵌套在Foreach循环中

时间:2015-05-05 19:39:40

标签: php wordpress foreach custom-fields short-open-tags

我有以下foreach循环:

<?php
    $fields = CFS()->get('list-item-field');
?>
    <? foreach ($fields as $field) : ?>

        <?= $field['list-item-title'] ?>    

    <? endforeach ?>

我想在循环中添加另一个foreach,如下所示:

<?php
    $fields = CFS()->get('item-field');
?>
    <? foreach ($fields as $field) : ?>

        <?= $field['list-item-title'] ?>

        <?php
            $values = CFS()->get('color');
        ?>
            <? foreach ($values as $value => $label) : ?>
                <? echo $value ; ?>
            <? endforeach ?>

    <? endforeach ?>

然而,这不起作用,我收到错误:

  

为Foreach()提供的无效参数

1 个答案:

答案 0 :(得分:4)

好吧我需要稍微过期,但我想出来了,我怀疑这会对很多人有所帮助,但不管这是我需要做什么:

<?php
    $fields = CFS()->get('item-field');
?>
    <? foreach ($fields as $field) : ?>

        <?= $field['list-item-title'] ?>

        <? foreach ($field['color'] as $colors => $label) :?>
            <? echo $colors ; ?>
        <? endforeach ?>

    <? endforeach ?>

这篇文章帮助:http://customfieldsuite.com/forums/questions/925/loop-within-a-loop