How to get custom field value with shortcode

How to get custom field value with shortcode

August 31, 2012 Custom field is a very useful functionality in wordpress. We can use it to show extra information on our blog post or page. I explain about shortcode in my previous post. If you are looking to build the shortcode for custom field, here is the solution: Put the below code into your functions.php /*************************/ add_shortcode('custom_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); } /*************************/ You can use the below shortcode to show the custom field value. [custom_field "custom_field_key"] [custom_field "custom_field_key" post_id=1]

Related Posts

from my blog


Why Should Your Hire Freelance Web Developer: The Top Benefits

May 17, 2022

When developing a website or an app, there are two major ways to do it - outsource the job to a web development company or hire a freelancer to get...

Improve The Reach of Your Business With WordPress Development Services

December 28, 2015

Social media marketing has undoubtedly changed the game between companies that are constantly in competition with each other. Not only has social media allowed better access to customer thought and...

Must Have Anti-Spam and Security WordPress Plugins

March 30, 2015

WordPress, is a hugely popular open source community platform which is also referred as a CMS. It's user friendly CMS that makes it very flexible and allows users to make...