Split WordPress Custom Field Values

We have been aware of WordPress’ Post Thumbnail Feature for a while now, but in some cases one is just not enough. This is a cool snippet for expanding WordPress’ post thumbnail output with the help of custom fields to add more thumbnails. Although there are plugin alternatives, this is a very quick method for something simple, short and sweet.

So lets say you have a portfolio page and you want to display a couple of thumbnails, say 4(or more). This is what you can do:

Create a custom field value of Images and in the textarea insert the links to your images. Each link MUST be on a new line. Then use this snippet.

The code above converts each new line to HTML’s <br /> tag and explodes this into an array containing the links to the images. You can then use a foreach loop to display these.

Code Snippet:

$images = get_post_meta($post-&gt;ID,âImagesâ);
$images = nl2br($images&#91;0&#93;);
$images = explode(â&lt;br /&gt;â,$images);
foreach($images as $src) {
	echo "<img src=\"".trim($src)."\" />";

Posted in Custom Fields