Working with custom fields

// July 7th, 2009 // Development

How to use Wordpress customfields is always useful to know and I found this on the very informative website perishablepress.com, so all credit to them for putting this together!

Source: http://perishablepress.com/press/2008/12/22/wordpress-custom-fields-tips-tricks/

Display values of a specific key

To loop through and display the values of a specific key, place the following within the loop of your choice, and change the “mood” value to that of your desired key value:

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

Display multiple values of a specific key

Each custom-field key may include multiple values. For example, if you listen to multiple songs during a given post, you may want to list them all with a key of “songs”. Then to loop through and display the multiple values for the songs key, we place the following code into the loop of choice:

<?php $songs = get_post_meta($post->ID, 'songs', false); ?>
	<h3>This post inspired by:</h3>
	<ul>
		<?php foreach($songs as $song) {
			echo '<li>'.$song.'</li>';
			} ?>
	</ul>

Notice the trick here: by changing the third parameter to “false”, we tell the function to return an array of the values for the specified key. A very handy trick for displaying multiple key values.

Display content only if a custom field exists

For cases when not all posts contain some specific custom-field key, use the following code to prevent unwanted, empty or incomplete markup from destroying the validity of your page:

// display an image based on custom-field value, if it exists

<?php $image = get_post_meta($post->ID, 'url', true);

	if($image) : ?>

	<img src="<?php echo $image; ?>" alt="" />

	<?php endif; ?>

Conditional display of custom-field data

Continuing with the previous technique, here is a basic code template for displaying a list of key values only if they exist:

<?php if(get_post_meta($post->ID, 'books', true) ||
	 get_post_meta($post->ID, 'music', true) ||
	 get_post_meta($post->ID, 'sites', true)
	 ): ?>

	<ul>
		<?php if(get_post_meta($post->ID, 'books', true)): ?>
		<li><?php echo get_post_meta($post->ID, 'books', true); ?></li>
		<?php endif; ?>

		<?php if(get_post_meta($post->ID, 'music', true)): ?>
		<li><?php echo get_post_meta($post->ID, 'music', true); ?></li>
		<?php endif; ?>

		<?php if(get_post_meta($post->ID, 'sites', true)): ?>
		<li><?php echo get_post_meta($post->ID, 'sites', true); ?></li>
		<?php endif; ?>
	</ul>

<?php endif; ?>

More conditional content based on custom-field values

Here’s another neat trick whereby custom-field values are used to determine which type of content appears on a page. In this example, we are checking the value of of a custom-field key called “hobbies”. Depending on the value of the hobbies key, different links are output on the page. Check it out:

<?php $value = get_post_meta($post->ID, 'hobbies', true);

	if($value == 'gaming') {
		echo '<a href="http://domain.tld/gaming/">Gaming Stuff</a>';
	} elseif($value == 'sleeping') {
		echo '<a href="http://domain.tld/sleeping/">Nap Supplies</a>';
	} elseif($value == 'eating') {
		echo '<a href="http://domain.tld/eating/">Dieting Advice</a>';
	} else {
		echo '<a href="http://domain.tld/">Home Page</a>';
	}

?>

Simplification and externalization

To clean up our source code a little, we can relocate the get_post_meta() function to the theme’s functions.php file. The immediate benefit here is one less parameter to include in the template tag. To do this, first place the following code into your theme’s functions.php file:

<?php function get_custom_field_data($key, $echo = false) {
	global $post;
	$value = get_post_meta($post->ID, $key, true);
	if($echo == false) {
		return $value;
	} else {
		echo $value;
	}
}
?>

..and then call the function by placing this code in the desired location within your page template:

<?php if(function_exists('get_custom_field_data')) {
	get_custom_field_data('key', true);
} ?>

The only thing you need to edit here is the value of the “key” parameter, which should be the same as the key for which you would like to display value data. The second parameter is currently set as “true” so that the key value is echoed to the browser. To save the key value as a variable for further processing, change this parameter to “false”.

  • Hi, I am looking for a way to display many images based on custom fields.
    I am working on a type of Image gallery where a post will have multiple images and I just don’t wanna have to insert them withing the post directly.
    Any help will be appreciated.
    By the way, I am modifying the theme “No-frills 2.1.7 by Jess Kim”

  • Awesome article about custom Wordpress fields and how to use them!

blog comments powered by Disqus