Arrays in fields
-
Is it possible to loop through an array saved to a field?
-
Currently, there’s no general way to loop through an array in a field.
However, there is already looping with arrays, for example, in attachments, gallery field, field types in ACF, metabox in WCK, etc., so I imagine it’s possible to implement it somehow.
Is this array saved as a simple list [1,2,3] or with key value pairs?
Saved as key value pairs. If you could loop through an array in the same manner as the existing [repeater] syntax that’d be a slick addition.
OK, I’ll look into it and let you know when I’ve got something.
That’d be great!
Another question: do you know the specific keys in this array? For example, I’m imagining something like this:
[field-array field_name] [field key_name1] [field key_name2] [/field-array]Would that work?
That would be perfect!
OK, please see the latest update. I simplified it to:
[array field_name] [field key_1] [field key_2] [/array]Hmm, looking at this, I realized it’s not really a “loop” since it goes through only once. I hope that’s what you needed!
Hmm I’m not seeing any result (I downloaded the latest update).
Perhaps a screenshot of the array would help:
http://i.imgur.com/4I0ALBT.gifFrom this array I would expect this code:
[loop type='location'] [array socialicons] [field socialicons_network]<br> [/array] [/loop]to produce:
facebook
twitter
google-plusHmm..looks like you have a field that’s structured like this:
Array ( [0] => Array ( [social_icons_url] => '/' [social_icons_network] => 'facebook' [social_icons_flyoutcontent] => 'etc.' ) [1] => Array.. [2] => Array....whereas currently [array] expects something simpler, like:
Array ( [key_1] => value_1 [key_2] => value_2 )Yeah..I was afraid that a shortcode to parse arrays can get complicated, since there are many different ways data can be stored in a field.
OK, I’ll add a parameter that will loop through each array in the field.
So, please try the latest update with:
[loop type="location"] [array socialicons each="true"] [field socialicons_network]<br> [/array] [/loop]That should loop through each array.
That works great, thank you!
The topic ‘Arrays in fields’ is closed to new replies.