admin管理员组文章数量:1320609
Everyone,
I have just started using ACF and mostly using feature to show data uploaded from csv file. Everything is working fine.
But in some fields i have data in the form of comma separated values such as Fiery Red, Leafy Green, Sky Blue.
My current output works as follows.
Available Colors - Fiery Red, Leafy Green, Sky Blue
I want to achieve;
Available Color - Fiery Red
Available Colors - Fiery Red, Leafy Green
Thank you !
Everyone,
I have just started using ACF and mostly using feature to show data uploaded from csv file. Everything is working fine.
But in some fields i have data in the form of comma separated values such as Fiery Red, Leafy Green, Sky Blue.
My current output works as follows.
Available Colors - Fiery Red, Leafy Green, Sky Blue
I want to achieve;
Available Color - Fiery Red
Available Colors - Fiery Red, Leafy Green
Thank you !
Share Improve this question edited Oct 3, 2020 at 23:04 seezar asked Oct 3, 2020 at 8:57 seezarseezar 53 bronze badges1 Answer
Reset to default 1You can use the explode()
function to split a string into the array of values
So you have a string of colors, separated with a comma, where the comma is a delimiter:
$colors = 'Fiery Red, Leafy Green, Sky Blue';
Now use the explode()
function (here is a link to the documentation) to convert it to the array:
$colors_array = explode( ', ', $colors );
As a result, you will get:
array(3)
(
[0] => string(9) "Fiery Red"
[1] => string(11) "Leafy Green"
[2] => string(8) "Sky Blue"
)
Now, when you have an array of values, you can display only the first value:
echo $colors_array[0];
or the first two elements:
$output_array = array_slice( $colors_array, 0, 2 );
echo implode( ', ', $output_array );
本文标签: advanced custom fieldsACFPick first or second value from comma separated values
版权声明:本文标题:advanced custom fields - ACF - Pick first or second value from comma separated values 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742083567a2419841.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论