default-blog

Get Custom Field Value with Shortcode

Get custom field to show inside current post or page in wordpress, just put this code inside function.php in your child theme

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]
Posted in ,

Leave a Comment