Grow your CSS skills. Land your dream job.

FOR loop with counter in variables

  • # May 19, 2013 at 9:31 pm

    So I have custom fields in WordPress but they are not all required, so they may or may not exist. There’s a maximum of 3, and I’ve named them product_image_1, product_image_2, product_image_3. etc.

    Right now, I have this to write them:

    the_field(‘product_image_1′);
    the_field(‘product_image_2′)’;
    the_field(‘product_image_3′)’;

    So I want to put this in a loop, and I thought this was the way to do it:

    for($i=1; $i < = 4; $i++) {
    the_field(‘${“product_image_” . $i}’);
    }

    But this doesn’t seem to work and it doesn’t write out anything but three times < img src="" >.

    # May 19, 2013 at 11:26 pm

    I think this is what you’re trying to do:

    < ?php
    # for 3 items, your loop should be from =1 to <4
    for( $i=1; $i<4; $i++ ){

    # write the item name as a string and pass it to your function
    the_field( “product_image_$i” );
    }
    }

    Note that I don’t know what that function *does*. This assumes you don’t need to know beforehand that each item exists.

    # May 20, 2013 at 9:23 am

    Indeed, I tried that, and I agree that’s what should work…..but it doesn’t give me back anything. I guess it just has something to do with the the_field that doesn’t want to play along.

    # May 20, 2013 at 9:35 am

    I’m guessing you’ve taken this into consideration, but in case not, traq’s PHP example has a stray closing curly bracer.

    # May 20, 2013 at 9:44 am

    Yea, noticed that, but that’s not the issue. I figured that it’s not the writing out of the string (that seems to work after all), it’s the part where I check if it exists. I did not included that in my original post cause I wanted to figure out the standard way of writing things first.

    So in essence, this works:

    for( $i=1; $i<4; $i++ ){
    echo get_field( “product_image_$i” );
    }

    And this works too:

    if (get_field(‘product_image_1′)) { echo get_field( “product_image_1″ ); }
    if (get_field(‘product_image_2′)) { echo get_field( “product_image_2″ ); }
    if (get_field(‘product_image_3′)) { echo get_field( “product_image_3″ ); }

    But this doesn’t:

    for( $i=1; $i<4; $i++ ){
    if (get_field(‘product_image_$i’)) { echo get_field( “product_image_$i” ); }
    }

    (FYI: get_field returns a simple value, in this case an image url)

    Bah :(

    # May 20, 2013 at 11:02 am

    for( $i=1; $i<4; $i++ ){
    $img_url = get_field( “product_image_$i” );
    echo ‘‘;
    }

    Maybe that will work ?

    # May 20, 2013 at 11:14 am

    Of course

    for( $i=1; $i<4; $i++ ){
    if (get_field(‘product_image_$i’)) { echo get_field( “product_image_$i” ); }
    }

    doesn’t work, you’re using single-quotes in it, which means that the field it’s trying to get is `product_image_$i`. There might be other issues going on (I’m mobile right now so can’t test it thoroughly), but that’s at least why that’s not working.

    # May 20, 2013 at 11:28 am

    Sorry about the stray `}`.

    > (FYI: get_field returns a simple value, in this case an image url)

    Are you sure about that? You’re using it as though it produces output directly.

    # May 20, 2013 at 12:12 pm

    the_field() outputs, get_field() does not.

    # May 20, 2013 at 12:22 pm

    > the_field() outputs, get_field() does not.

    So, `get_field()` returns its value?

    What does it return if there is no value?

    # May 20, 2013 at 12:23 pm

    Ok now I’m even confusing myself.

    @thedoc: I thought get_field() returna variable, and the_field() writes it out? Or in words, echo get_field() does the same as the_field()…. Right?

    On mobile now but I will explain a little more when I get back.

    # May 20, 2013 at 12:58 pm

    You would do something like this:

    $my_var = get_field(‘some_field’);

    if( $my_var ) {
    echo $my_var;
    }

    Or, if you want to output the field without checking if it exists or not, you’d go like this:

    the_field(‘some_field’);

    > Or in words, echo get_field() does the same as the_field()…. Right?

    Basically, yea!

    # May 20, 2013 at 2:35 pm

    Does this work for you?

    < ?php
    # for 3 items, your loop should be from =1 to <4
    for( $i=1; $i<4; $i++ ){

    # write the item name as a string and pass it to your function
    the_field( ‘”product_image_’.$i.’”‘ );
    }

    Are you using advanced custom fields?

    # May 20, 2013 at 3:02 pm

    > `the_field( ‘”product_image_’.$i.’”‘ );`

    too many quotes.

    If `get_field()` **returns** a value, or false if there is no value, then you can do like so:

    < ?php
    for( $i=1; $i<4; $i++ ){

    # note: double-quotes around whole string
    $var = get_field( “product_image_$i” );

    # assuming you want to output *now*
    if( $var ){ echo $var; }
    }

    # May 20, 2013 at 4:49 pm

    @dclardy: yep, advanced custom fields.

    @traq: yup, that works! Finally, thanks.

    I think the whole confusion is rooted in the way quotes have to be used. It sure confused me, because of the double quotes (see @melindrea‘s comment), but it does seem the way to go. This is what I’ll end up with, although I can’t fully wrap my head around why it’s working….

    for( $i=1; $i<4; $i++ ){
    if( get_field( “product_image_$i” ) ){ echo get_field( “product_image_$i” ); }
    }

Viewing 15 posts - 1 through 15 (of 18 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".