If you are storing your meta values as a single string separated by commas, you can use the native PHP explode()
to store them in an array:
$ct = get_post_meta($post->ID, 'ct', true);
// Store them in an array
$country_array = explode(',' , $ct);
// Run a loop and echo them
foreach( $country_array as $key => $country ){
echo "<span>{$country}</span>";
echo ( ( $key < ( count( $country_array ) -1 ) ) ? ', ':'' );
}
This might cause issue if you have a value like this:
Hong kong, China
Better yet save them as an array, instead of a single string.
Even better is to use custom taxonomy named country
, and then use the_terms()
to output them. This function allows you to add separator and before & after texts:
the_terms( $post->ID, 'country', '<span>', ',', '</span> );