phpwordpressforeachcustom-fieldsphp-shorttags

Nesting Foreach loop inside Foreach loop using short_open_tags


I have the following foreach loop:

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

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

    <? endforeach ?>

And I would like to add another foreach inside the loop, like so:

<?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 ?>

However this doesn't work, and I get the error:

Invalid Argument Supplied For Foreach()


Solution

  • Alright I needed to expirement a bit but I figured it out, I doubt this will be helpful to many but regardless here's what I needed to do:

    <?php
        $fields = CFS()->get('item-field');
    ?>
        <? foreach ($fields as $field) : ?>
    
            <?= $field['list-item-title'] ?>
    
            <? foreach ($field['color'] as $colors => $label) :?>
                <? echo $colors ; ?>
            <? endforeach ?>
    
        <? endforeach ?>
    

    This post helped: http://customfieldsuite.com/forums/questions/925/loop-within-a-loop