John Davidson

php - Explode comma separated values to append anchor href for each value

0 comments
Message:


Comma seprated values are stored in a wordpress field. Now I want to echo out all the comma seprated values into anchors <a href="value1">value1</a> <br> <a href="value2">value2</a> etc. Number of comma seprate values shold be same a appended anchors. (It should not show any space in next value, if there is space after comma)


I have tried it by using,


$values = explode(',', ''. get_post_meta( $order->id, '_xrd_ref', true ) .'');
foreach ($values) {echo '<p><a href="https://example.com/'. $values .'">'. __( '. $values .' ) . '</a><p>';}


My Answer:
// Get the comma separated values from the wordpress field
$values = get_field('comma_separated_values');

// Explode the values into an array
$items = explode(',', $values);

// Loop through each value and echo out as anchor tags
foreach($items as $item) {
echo '' . trim($item) . '
';
}
?>

Rate this post

3 of 5 based on 2912 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us