I have a php implode function to list wordpress custom field keys which works great. The problem is I need to wrap the web urls in an href to make them clickable and can't wrap my head around the function sytnax. The key value is in this format www.site.com, so I had it set up for a single entry like so:
<?php if(get_post_meta($post->ID, 'website', true)): ?>
<strong>Website:</strong> <a href="http://<?php echo get_post_meta($post->ID, 'website', true); ?>" target="_blank"><?php echo get_post_meta($post->ID, 'website', true); ?></a>
<?php endif; ?>
but now we need to be able to hold multiple entries separated by a comma. Here is the code that works, but does not output a clickable url:
<?php
if( $website = get_post_meta($post->ID, 'website') ):
$label = count( $website ) > 1 ? 'Websites' : 'Website';
?>
<strong><?php echo $label; ?>:</strong> <?php echo implode( $website, ', ' ); ?><br />
<?php
endif;
?>
This is what I've been playing with, which is clearly wrong
<?php echo '<a href="http://' . implode('" target="_blank">', $website) . "</a>" . ', '; ?><br />
Even if it did work, it would only output the url, not the text that is linked.
--------------------- EDIT -------------------------
Kai's answer was closest, so I marked it the answer, but it didn't include the label variable. By marrying two of them I came up with this answer which works beautifully
<?php
if( $website = get_post_meta($post->ID, 'website') ):
$label = count( $website ) > 1 ? 'Websites' : 'Website';
$links = array_map(
function($url) {
$url = htmlspecialchars($url);
return sprintf ('<a href="http://%s">%s</a>', $url, $url);
},
$website);
?>
<strong><?php echo $label; ?>:</strong> <?php echo implode(', ', $links); ?><br />
<?php endif ?>