Get custom field value with shortcode

Adding this PHP code to the functions.php of your wordpress theme will enable you to get custom field values using shortcode.

 

 

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);
}

 

[field "my_key"]
[field "my_key" post_id=1]

Be Sociable, Share!

Leave a comment