- This topic is empty.
-
AuthorPosts
-
May 19, 2013 at 9:31 pm #44889SenffParticipant
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 #135659__ParticipantI 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.
May 20, 2013 at 9:23 am #135698SenffParticipantIndeed, 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:44 am #135702SenffParticipantYea, 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:28 am #135718__ParticipantSorry 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 #135723TheDocMemberthe_field() outputs, get_field() does not.
May 20, 2013 at 12:22 pm #135726__Participant> 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 #135727SenffParticipantOk 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 #135662TheDocMemberYou 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 #135742dclardyMemberDoes 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?
May 20, 2013 at 3:02 pm #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; }
}May 20, 2013 at 4:49 pm #135759SenffParticipant@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” ); }
}May 20, 2013 at 6:48 pm #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”May 20, 2013 at 7:03 pm #135780AlenParticipantYou 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.
May 20, 2013 at 8:57 pm #135794SenffParticipant> 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.
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.