Created
September 3, 2015 07:29
-
-
Save ovizii/8fc617eb6a094792d4bc to your computer and use it in GitHub Desktop.
Get a custom field value through shortcodes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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