How to Get a Custom Field Value via Shortcode

Sometimes we want to make it easy for site administrators to include the value of a custom field (maybe created as part of a custom meta box) into a post. An ideal solution to this challenge is to use shortcodes. You can either do this through these two plugins: Get Custom Field Values Custom Fields Shortcode (the simpler of the two) Or else use a custom solution by building your own plugin or inserting code into your functions.php file. In that case you can take a look at this snippet of code and modify it to your needs. It’s pretty straightforward if you know how shortcodes work. If you need a shortcodes primer, check out this tutorial. I usually go the manual route when building a site for a client, and also package it in a plugin, so functionality won’t be broken if we design a new theme for him in the future. So for example, if I wanted to get a known custom field, this code would do the trick: [php] function get_custom_field_fn() { global $post; $custom_field_value = get_post_meta($post->ID, ‘cf_name’, true); return $custom_field_value; } add_shortcode(‘cf_name’, ‘get_custom_field_fn’); [/php] If you want to keep it more flexible and define the name of the custom field on the fly, you can use something slightly more advanced: [php] 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); } [/php] If you enjoyed this post, make sure to subscribe to WPMayor’s RSS feed. Jean Galea Jean Galea is an investor, entrepreneur, and blogger. He is the founder of WP Mayor, the plugins WP RSS Aggregator and Spotlight, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com. Related Topics: Plugin Reviews Consider sharing this post so others can find it: Share on facebook Share on twitter Share on linkedin Share on reddit Share on telegram Share on whatsapp Share on pocket Share on email Join thousands of people receiving real-world, genuine evaluations of WordPress products and services just like this one every week. Email Stay informed for free

Share:

Facebook
Twitter
Pinterest
LinkedIn

Leave a Comment

Your email address will not be published.

Table of Contents

On Key

Related Posts

Best Google Maps Plugins for WordPress

Before we start looking at any plugins, it’s good to review the way Google Maps work. There are several components in the Google Maps API family, all of which can be found on the Google Maps API Family page. When working with WordPress, we are mainly interested in two of these APIs: Static Maps APIMaps

How to Get a Custom Field Value via Shortcode

Sometimes we want to make it easy for site administrators to include the value of a custom field (maybe created as part of a custom meta box) into a post. An ideal solution to this challenge is to use shortcodes. You can either do this through these two plugins: Get Custom Field Values Custom Fields

Best Auto Translation Plugins for WordPress

Having the content of your website available in different languages can mean a much bigger audience for you. A very simple way to do this is to provide automatic translators, they’re not the same as manually translating all your content, but do a pretty good job. It’s better to have an automatic translation plugin than

Best WordPress Multilanguage Plugins

The two main contenders as best multilanguage plugins for WordPress are WPML and QTranslate. If you want a quick answer to which one comes out on top, I would say go for WPML. If you have enough time to give them a test drive, by all means, do so and judge for yourself which one