WordPress: Get custom field function

On several WordPress projects I've needed to get the value of a custom field.  Not too difficult using the get_post_meta command, but I wanted a little quicker way.  So I now add this little function to the functions.php file on themes I'm working on.

PHP:
  1. function getMeta($key)
  2. {
  3. global $post;
  4. return get_post_meta($post->ID, $key);
  5. }

Say you've got a custom field name thisIsMyCustomField, you'd just do this to display the value.

PHP:
  1. echo getMeta('thisIsMyCustomField');

Question, Comments...

Do you have more questions. Please either leave a comment below or join us in our new forum.

Leave a Reply