Custom Fields in WordPress Pages… the Easy Way

Custom Fields have made turning WordPress into a great and powerfull CMS… if you know how to use them that is. Even the explanation WP.org gives about custom fields is fairly vauge and not very easy to figure out. Nathan Rice over at (ironic) NathanRice.com has post some quick tips on how to quickly and easily setup custom fields for your page templates.

Nathan tells us to first add this code to your functions.php file in your current themes folder. If you don’t have a functions.php just create it. Theres no real format to this file other than it needs to be in your current themes folder.

 

function get_custom_field($key, $echo = FALSE) {
	global $post;
	$custom_field = get_post_meta($post->ID, $key, true);
	if ($echo == FALSE) return $custom_field;
	echo $custom_field;
}

Now all you have to add into your page template file is the following code for each instance that you want to display your custom field wether it be the same custom field several times or different custom fields.

<?php get_custom_field('image', TRUE); ?>

One note, this code must be inside your loop for it to work. No biggie… just normal stuff right.

~ by coderdan on October 15, 2008.

Leave a Reply