Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End FOR loop with counter in variables

  • This topic is empty.
Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #44889
    Senff
    Participant

    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="" >.

    #135659
    __
    Participant

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

    # 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.

    #135698
    Senff
    Participant

    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.

    #135702
    Senff
    Participant

    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 :(

    #135718
    __
    Participant

    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.

    #135723
    TheDoc
    Member

    the_field() outputs, get_field() does not.

    #135726
    __
    Participant

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

    So, `get_field()` returns its value?

    What does it return if there is no value?

    #135727
    Senff
    Participant

    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.

    #135662
    TheDoc
    Member

    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!

    #135742
    dclardy
    Member

    Does this work for you?

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

    #135747
    __
    Participant

    > `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:

    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; }
    }

    #135759
    Senff
    Participant

    @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” ); }
    }

    #135778
    __
    Participant

    > I think the whole confusion is rooted in the way quotes have to be used. It sure confused me

    Remember, single-quotes ( `’` ) make literal strings – every character you type ( except “, but let’s not get into that right now) will be exactly as you see it.

    With double-quotes ( `”` ) , PHP will interpret variables and escape characters in the string. So,

    $var = ‘world’;
    echo ‘Hello, $var’; # prints “Hello, $var”
    echo “Hello, $var”; #prints “Hello, world”

    #135780
    Alen
    Participant

    You can also be explicit by using `{}` like so:

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

    This way when you’re reading PHP code, it’s clear what is happening or what is expected to happen.

    #135794
    Senff
    Participant

    > Remember, single-quotes ( ‘ ) make literal strings – every character you type ( except , but let’s not get into that right now) will be exactly as you see it.

    > With double-quotes ( ” ) , PHP will interpret variables and escape characters in the string

    Ah, of course….one of the things in PHP that I completely forgot about. Fiddling with JS so much (where single quotes and double quotes are used depending on how I nest code within code usually…) made me forget all about that.

Viewing 15 posts - 1 through 15 (of 15 total)
  • The forum ‘Back End’ is closed to new topics and replies.