Skip to content

Instantly share code, notes, and snippets.

@ovizii
Created September 3, 2015 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ovizii/8fc617eb6a094792d4bc to your computer and use it in GitHub Desktop.
Save ovizii/8fc617eb6a094792d4bc to your computer and use it in GitHub Desktop.
Get a custom field value through shortcodes
add_shortcode('field', 'shortcode_field');
function shortcode_field($atts){
extract(shortcode_atts(array(
'post_id' => NULL,
), $atts));
if(!isset($atts[0])) return;
$field = esc_attr($atts[0]);
global $post;
$post_id = (NULL === $post_id) ? $post->ID : $post_id;
return get_post_meta($post_id, $field, true);
}
//Usage:
//[field "my_key"]
//[field "my_key" post_id=1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment