customfields-spllit

How to Split and Categorize WordPress’ Custom Field values

Posted in Web Design/Development, Wordpress | 27 Comments

We have been aware of WordPress’ Post Thumbnail Feature for a while now, but in some cases one is just not enough.  This tutorial was inspired by a blog post by David Leggett of Tutorial9.net. This is an advanced tutorial in 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.

Scenario 1

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:

STEP 1

Download this plugin 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.

customfield-thumbs

Before we create an array containing the images, we have to make sure that the linebreaks are properly recognised as at this moment linebreaks are converted to white space when placed within the WordPress custom fields box.

STEP 2

In the WordPress loop place this code:

<?php
$images = get_post_meta($post->ID,‘Images’);
$images = nl2br($images[0]);
$images = explode(‘<br />’,$images);
?>

The code above uses WordPress’ get_post_meta function to retrieve the custom field key. It then converts the linebreaks to html’s <br /> tag using PHP’s nl2br() function. We then create an array with the explode() function using the <br /> tag as the separator of the different lines of data. Using the print_r() php function we can see the array as such:

print_r($images);
//Lists the result below
/*
Array
(
[0] => http://localhost/path/to/file/treeinfall.jpg
[1] => http://localhost/path/to/file/treecover.jpg
[2] => http://localhost/path/to/file/yellowflower.jpg
[3] => http://localhost/path/to/file/sunflowers.jpg
)
*/

STEP 3

We done the main bit, but as you’ve guessed by now, we now have to echo our results. To do that simply add this code below the previous:

foreach($images as $src)
{ echo "<img src=\"".trim($src)."\" />";
}

And that’s pretty much it.  The trim() function simply chops off any spaces surrounding the image.

View output.

Scenario 2

So you are sorting out a mini item display linking to your bigcartel store online and you want something simple to display, linking to the main product page. You are intending to display:

  • Item Name
  • Item Image
  • Price
  • Link to Main Item Page

STEP 1

Create a couple of custom fields with the key ItemInfo and enter your information in the same order as the bulleted list above.

alt

STEP 2

We can start by simply adding this code to our single.php file:

$items = get_post_meta($post->ID, 'ItemInfo');

Using the print_r() function will echo out the results as follows:

/*
Array
(
[0] => Treeinfall Photo http://localhost/path/to/uploads/treeinfall.jpg £10.00 This is brilliant Photography. Purchase!

[1] => Treecover Photo http://localhost/path/to/uploads/treecover.jpg £8.00 Strong Photography. Purchase Now!

[2] => Yellow Flower Photo http://localhost/path/to/uploads/yellowflower.jpg £12.00 Brighten up your day. On Sale!!

[3] => Sunflowers http://localhost/path/to/uploads/sunflowers.jpg £10.00 Millions of Sunflowers. All Yours
)
*/

STEP 3

Like I stated before the linebreaks are masked as spaces, which we will have to convert to linebreaks like the previous function. This time we are going to introduce the list() function, which allows us to assign variable names to our array keys.

foreach($items as $item) {

$item = nl2br($item);
list($name,$src,$price,$desc) = explode('<br />',$item);

echo "<div class=\"item\">";
echo "<h2>$name</h2>";
echo "<img src=\"".trim($src)."\" />";
echo "<br />Price: $price";
echo "<br />Description: $desc";
echo "</div><br />";

}

What we have done is simply used a foreach loop to display our results.

And VOILA!!!

An idea you could utilise is creating some custom write panels that will have these fields displayed already instead of creating them from scratch. It will be more user friendly and easier for the client to utilise.

If this post has helped you, please leave a comment. I will try to answer any questions you may have or problems you experience.

Further Reading:



27 Comments to “How to Split and Categorize WordPress’ Custom Field values”




























Recommended

Take Your WordPress Skills to the Next Level - Digging Into WordPress by Chris Coyier and Jeff Starr
Learn how you can market your business on Twitter
Get the Book now! Use 25% discount code 'november25'
Hostgator - Cheap and Reliable Web Hosting - WordPress friendly

Incoming Tweets