Like the blog? Get the book »

Splitting Up Custom Field Values

I’ve worked on sites in the past where I needed to get two values out of a custom field. For example, I was creating an eCommerce site where some products had options. The options were in a dropdown menu. Each option in the dropdown had a value for what you actually see, and then another value for how that option effected the price. When entering a custom field, I often did it like this:

Splitting Up Custom Field Values

Then in the template file, I pulled in the custom fields and split them apart using PHP’s explode() function based on the | character. Works fine really, but the pipe character is rather clunky to use in that way. Jermaine Oppong over at Graphicbeacon shows how he has done it by using linebreaks in the value instead, then using PHP’s nl2br() and exploding based on <br />.

Editor’s note: 404 link removed.

© 2009–2024 Digging Into WordPress Powered by WordPress Monzilla Media shapeSpace