Nowadays, most modern WordPress themes uses custom fields, to display a thumbnail near the post excerpt, or for adding any other kind of data. Here’s a custom function, created by Matt Varone, to easily get any custom field value.
Nowadays, most modern WordPress themes uses custom fields, to display a thumbnail near the post excerpt, or for adding any other kind of data. Here’s a custom function, created by Matt Varone, to easily get any custom field value.
Here's the function. You have to paste it on your theme functions.php file. If your theme doesn't have a file named functions.php, create one.
function get_custom_field_value($szKey, $bPrint = false) {
global $post;
$szValue = get_post_meta($post->ID, $szKey, true);
if ( $bPrint == false ) return $szValue; else echo $szValue;
}
Now, to call the function and get your custom field value, use the following code:
<?php if ( function_exists('get_custom_field_value') ){
get_custom_field_value('featured_image', true);
} ?>
First, we use the php function_exists() function to make sure the get_custom_field_value function is defined on our theme. If it is, we use it. The first argument is the custom field name (here, featured_image) and the second let you echo the value (true) or get it for further use in php (false).
Thanks to Matt Varone for this awesome piece of code!
29 Responses
Really usefull ! Thanks for the tips
bounced over from smashing mag – good stuff – keep cooking..
Thank you guys!
Thank for this! I have always wondered how people add custom fields to their themes
As usual great code, Jean. Please correct me if I’m wrong, the only element I need to change is ‘featured_image’ if I were to use say, ‘thumbnail’….is that right?
Yan
@Blog for beginners: Exactly, just change “featured_image” by he name of you custom field
Thanks for the clarification, Jean.
Yan
You’re welcome
Again very nice recipe, I’ll try to make use of this in soon time after I got a permanent theme for my blog and for my other blog, it can help out adding some special details on selected posts etc etc.
Do you need to put it in the loop?
@Ninon: Yes.
This is a very important tip. It can make WP much more powerful and flexible with Custom Fields to display anywhere you want. Very good. Thank you again. I already have this working before this post but I like to see more discussion of techniques for using this.
Very useful, and I’m using it!
One question though.. I’m using it to show dates of tours/trips. Some trips have more than one date custom field associated with them (date1, date2, date3, etc). How could I test for a non-empty custom field? In other words:
Show date1
If not empty show date2
If not empty show date3
Gary
Why not just use
ID, “website”, true); ? >
I use this…
ID, “XYZ”, true) ): ? >
ID, “XYZ”, true); ? >
and if i need to make this custom field (image-wide) as invisible (deactive) in a page template, that contains the custom field? what i must do? eg: pageA i want to use the custom field. but in pageB not. in FF is ok. but in IE, appears a box with X. thanks!
Shit! This is good!
Trackbacks: