Display WP-Types custom fields in post [closed]

As you may or may not know any post meta data is stored in a new table, $wpdb->postmeta. This table has four fields:

  • ‘meta_id’ – A unique id for each entry.
  • ‘post_id’ – The ID of the post for this metadata.
  • ‘meta_key’ – The name of the ‘key’.
  • ‘meta_value’ – The value associated with the key.

To use metadata in your theme use the get_post_meta() function.

get_post_meta($post_id, $key, $single);

How that works is:

  1. $post_id is the ID of the post you want the meta values for. Use $post->ID to get a post’s ID within the $post variable scope. Use get_the_ID() to retrieve the ID of the current item in the WordPress Loop.
  2. $key is a string containing the name of the meta value you want.
  3. $single can either be true or false. If set to true then the function will return a single result, as a string. If false, or not set, then the function returns an array of the custom fields.

And to use that in a shortcode add this to your child themes functions.php:

function your_function_name( $atts ) {
    $a = shortcode_atts( array(
        'id' => get_the_ID(),
        'key' => '',
        'single' => 'true',
    ), $atts );

    return get_post_meta($a['id'], $a['key'], $a['single']);
}
add_shortcode( 'shortcode_text', 'your_function_name' );

The above function would create this shortcode: [shortcode_text id="" key="" single=""].

As I said before if you want to retrieve the ID of the current item in the WordPress Loop leave out id="" altogether as I set the default value to be get_the_ID().
And if you only want to get a single value you can leave out single="" as well as the default is set to true.

Custom Fields

Shortcode API