Websnippetz

Get cool snippets on php, css, jQuery, Magento and wordpress

Get custom field value through shortcode

Creating wordpress short-codes is simple. We can access custom field value through shortcode, just follow the function.


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]

karthi s

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: