I have a repeating field "contact" where people input data in the following format
"firstname lastname | info@example.com"
Now I want to have a shortcode that gets the array with the data and checks if there is a "|" symbol and if yes it outputs the data in another format.
function func_contacts($atts) {
global $post;
$contact = get_post_meta( $post->ID, 'wpcf-' . 'contacts' , true );
foreach ($contact as &$value) {
if ( strpos($value, "|") ) {
$subarray = explode('|', $value);
$conty .= $subarray [0] . " E-Mail: " . $subarray [1] . "<br>";
} else {
$conty .= $value."<br>";
}
}
return $conty;
}
add_shortcode( 'show-contact-data', 'func_contacts' );
$contact seems to not be an array and contain only the first instance of the repeating field.
I am lost.
None of the docs here helped me with retrieving data from repeating fields:
https://toolset.com/documentation/programmer-reference/views-api/#render_view
https://toolset.com/forums/topic/how-to-display-repeatable-field-groups-with-php/
https://toolset.com/documentation/customizing-sites-using-php/displaying-repeating-fields-one-kind/
I just had to change the first line from:
$contact = get_post_meta( $post->ID, 'wpcf-' . 'contacts' , true );
to
$contact = get_post_meta( $post->ID, 'wpcf-' . 'contacts' , false );
To retrieve more than one instance. Silly me.
My issue is resolved now. Thank you!