Making use of Custom Fields

Custom fields in WordPress are a great way to add post/page-specific content outside of the main content area. Here’s a rundown of php snippets I use to get custom fields displayed.

<?php
	$info = get_post_meta($post->ID, 'side_info');
	if (is_array($info) && !empty($info)) {
		foreach($info as $bit) { 	echo $bit; }
	}

(above) get the data from the custom field named “side_info” and spit it out. Plain and simple. If there are multiple custom fields with that name defined, it’ll cycle through each of them and spit each one out one after the other. Custom fields will not make your data ‘pretty’ so include any necessary HTML tags.

<?php
	$info = get_post_meta($post->ID, 'side_info');
	if (is_array($info) && !empty($info)) {
		foreach($info as $bit) { 	echo $bit; }
		break;
	}

(above) The break; line is my cheat for only displaying the first (in case there are multiple) custom field values.

<?php
	$info = get_post_meta($post->ID, 'side_info',true);
	echo $info;

(above) Adding the true argument to the get_post_meta() function forces the result to be returned as a string rather than an array (default). If you know you’ll only have on value for each unique custom field, this is probably the way to go.

<?php
	$photo = get_post_meta($post->ID, 'hd_photo');
	if (is_array($photo) && !empty($photo)) 	foreach($photo as $img) { 	echo '<img src="'.$img.'" alt="" />'; }
	else	echo '<img src="'.get_bloginfo('template_directory').'/images/hd_photo.jpg'.'" alt="" />';

(above) If I know that I’m using a custom field solely for images, I’ll use the field to store that path only and generate the <img> behind the scenes.

<?php
	$side_info = get_post_meta($post->ID, 'side_info');
	$info = '';
	foreach($side_info as $bit) {
		if (strpos($bit,'#')) {
			$bit = explode('#',$bit);
			$info .= '<img src="'.$bit['0'].'" title="'.$bit['1'].'" alt="" />';
		}
		else {
			$info .= '<img src="'.$bit.'" alt="" />';
		}
	}
	echo $info;

(above) If I’m feeling particularly fancy, I’ll store the path and title together with some sort of separator (ie. /images/whiskers.jpg#My cat Whiskers) and use php to split that data. In the example, I first check to see if the separator character exists, and if it doesn’t, I deliver the data as is.

<?php
	$side_info = get_post_meta($post->ID, 'side_info');
	$info = '';
	if (is_array($info) && !empty($info))
	foreach($side_info as $bit) {
		if (substr($bit, -4) == '.jpg')
			if (strpos($bit,'#')) {
				$bit = explode('#',$bit);
				$info .= '<img src="'.$bit['0'].'" title="'.$bit['1'].'" alt="" />';
			}
			else {
				$info .= '<img src="'.$bit.'" alt="" />';
			}
		else
			$info .= $bit;
	}
	echo $info;

(above) If your custom field will contain different kinds of data (text, images, etc…) you could even throw in a little auto-detection and process the data accordingly.

3 thoughts on “Making use of Custom Fields”

  1. Going to try these… Looks like some amazing stuff, I’m new to web-design and like just working on my own websites though, and I use wordpress mainly. So your site should help a ton, would you happen to be willing to share your skype and gmail? So that I could stay in contact? If not I understand…

    Peace, & Blessings

    -IZZY

  2. Glad you like what I’ve posted so far – hopefully comments like these will motivate me to keep this updated.

    I’m not a huge fan of sharing contact details – but feel free to contact me on twitter: http://twitter.com/trepmal

  3. Is it possible to use custom fields on a Page to put a line of images above the title? I tried using the command precontent in the value field and just pasting the html for the table i wanted above the title in the custom fields box but nothing at all showed up. What I want is a line of pictures of products above the title. I can use tables to make it but I cant figure out how to get it above the title. If you have any ideas I’d be grateful:)

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: