Skip to content

Instantly share code, notes, and snippets.

@trovster
Created April 19, 2012 11:09
Show Gist options
  • Select an option

  • Save trovster/2420303 to your computer and use it in GitHub Desktop.

Select an option

Save trovster/2420303 to your computer and use it in GitHub Desktop.
Simple custom get custom field function, which handles when a key doesn't exist.
<?php
function template_get_custom_field($custom, $key, $prefix = 'custom_') {
if(!empty($custom[$prefix . $key][0])) {
return $custom[$prefix . $key][0];
}
return '';
}
// examples
$custom = get_post_custom($post->ID);
template_get_custom_field($custom, 'name'); // looks for 'custom_name'
template_get_custom_field($custom, 'thumbnail_id', ''); // looks for 'thumbnail_id'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment