Use the following example code to get a custom field value through shortcodes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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:
1. [field "my_key"]
2. [field "my_key" post_id=1]
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.