0

I am trying to master the art of the foreach loop; I have the following code which I am using with WordPress and the Advanced Custom Fields pluging. I want to turn it into a foreach loop.

<li data-thumb="<?php the_field('image_1'); ?>"> 
    <img src="<?php the_field('image_1'); ?>" />
</li>
<li data-thumb="<?php the_field('image_2'); ?>"> 
    <img src="<?php the_field('image_2'); ?>" />
</li>
<li data-thumb="<?php the_field('image_3'); ?>"> 
    <img src="<?php the_field('image_3'); ?>" />
</li>
<li data-thumb="<?php the_field('image_4'); ?>"> 
    <img src="<?php the_field('image_4'); ?>" />
</li>
<li data-thumb="<?php the_field('image_5'); ?>"> 
    <img src="<?php the_field('image_5'); ?>" />
</li> 

I have tried writing the code below but it doesn't work, and I don't know how to limit the loop to 5 (images). Note that get_field returns the image url whilst the_field returns the image.

<?php                       

    $i=1;
    foreach (!empty (get_field('property_image.$i.')) ) {

    print (' <li data-thumb="<?php the_field('property_image'.$i.'); ?>"> 
             <img src="<?php the_field('property_image'.$i.'); ?> "> 
             </li> ');

    $i++; 

        }

?>

5 Answers 5

3

If you know that there are only 5 items, then you would just use a for or while loop. foreach is a loop designed for looping through an array of elements, which you don't have.

Consider this loop instead:

for($i = 1; $i <= 5; $i++) {
   if( !empty(get_field('property_image'.$i)) ) {
     echo '<li data-thumb="' . the_field('image_' . $i) . '">';
     echo '<img src="' . the_field('image_' . $i) '" />';
     echo '</li>';
   }
}
Sign up to request clarification or add additional context in comments.

Comments

1

foreach is used to iterate over arrays, e.g.

foreach (array_expression as $value) {
     // current array element
}

The syntax you're using will not work with foreach (see examples to understand how it works).

Comments

0

For the implementation you posted, you would be better off using a normal for loop.

for ($i = 1; $i <= 5; $i++) {
    // print <li>
}

To use a foreach loop, you need to have iterate over an array. get_field("name") does not return an array, however you CAN use foreach with get_fields()

$fields = get_fields();

if( $fields )
{
    foreach( $fields as $field_name => $value )
    {
        // Output values here
    }
}

Details here: http://www.advancedcustomfields.com/resources/get_fields/

Comments

0

In your case for loop is better as changed in the loop value is numeric. So solution will be like:

for ($i = 1; $i<=5; $i++) {
    $src = the_field('image_'.$i);
    printf('<li data-thumb="%s">', $src);
    printf('<img src="%s" />', $src);
    print('</li>');
}

If you still want to use foreach loop then you can use built-in php function range to get required numbers.

foreach (range(1, 5) as $i) {
    $src = the_field('image_'.$i);
    printf('<li data-thumb="%s">', $src);
    printf('<img src="%s" />', $src);
    print('</li>');
}

Comments

0

What you probably wanted to write was a while loop up there. Foreach loops don't test a condition before a cycle. Rather, foreach loops take an array and iterate over all the values within. It can also iterate over associative arrays.

<?php
$users = array(
    'user_mango' => 'John Doe',
    'user_2' => 'Jacob Doe',
    'user_potato' => 'Jane Doe'
);

foreach ($users as $user_id => $name) {
    echo $user_id, ' - ', $name, '<br>';
}

should output

user_mango - John Doe
user_2 - Jacob Doe
user_potato - Jane Doe

I'm not a wordpress developper but if you wanted to write this code in the style you started off with, here goes:

<?php
$i = 0;
while (!empty(get_field('property_image.$i.')) && $i < 5) {
    echo 'YOUR TEMPLATE CODE';
    $i++;
}

while loops, unlike foreach loops, test a condition before each iteration. Here in the above example code, we have our counter variable initialized to zero. Inside the loop we increase the counter variable by one on each iteration, and in the condition, we specify that in order for the full expression to be true the counter must be smaller than 5.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.