Quantcast
Channel: WordPress Tips Archives - Nathan Rice
Viewing all articles
Browse latest Browse all 15

An Easy Way To Get the Contents of a Custom Field

$
0
0

If you do any hardcore WordPress coding, then you’re probably aware of Custom Fields and the unlimited possibilities they offer you as a WordPress designer or developer.  For instance, I use custom fields as a means of attaching a feature image and thumbnail to posts in my Proximity News Theme.

But, they can be a bit cumbersome to use throughout your theme because of the ridiculous amount of code that it takes just to pull the data out and display it.

Conventionally, here’s how you would pull the data from a custom field where the key = image and use the value of that field as the “src” value in an IMG tag:

<?php $image = get_post_meta($post->ID, 'image', TRUE); ?>
<?php if($image) { ?><img src="<?php echo $image; ?>" alt="Alt Text" /><?php } ?>

Putting the value from the custom field into a variable definitely cuts down on the amount of code you have to write, but by utilizing a simple PHP function, we can make using custom fields even easier!  Open up your theme’s functions.php file and paste the following code somewhere between PHP tags:

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, when you want to get the value of the custom field, you simply use the function in your theme files like so:

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

Using the “TRUE” value makes sure that the function actually echos the value, rather than just returning it.  But if for some reason, you need to use the value of the custom field (for instance, to store in a variable), then you can return the value instead of echoing it like so:

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

Also, you could change the value of the $echo varialble in the function declaration to TRUE if you would like the default for the function to be to echo the value, rather than return it. It’s really up to your preferences.

Now, let’s take it one step further.  Let’s recycle some code from our get_custom_field function and use it in a function that will check to see if that custom field has a value — and if it does, then to use that value, along with the proper IMG tags to output an image, including width and height specifications:

function image_attachment($key, $width, $height) {
	global $post;
	$custom_field = get_post_meta($post->ID, $key, true);

	if($custom_field) { //if the user set a custom field
		echo '<img src="'.$custom_field.'" alt="" width="'.$width.'" height="'.$height.'"/>';
	}
	else { //else, return
		return;
	}
}

Then, just use the function in your theme files.  This example would echo the image with a width and height of 100px:

<?php image_attachment('image', 100, 100); ?>

Cool, huh? :-)  Feel free to experiment with the many, many opportunities these kinds of shortcuts can bring you.


Viewing all articles
Browse latest Browse all 15

Trending Articles