Add Content from a Custom Field to a Layout

From IThemes Codex
Jump to: navigation, search

If you want to add the content of a custom field to your layout, add the following code at the end of your functions.php file (but before the closing ?>, if any).

Code

function get_my_custom_field() {
    global $post;
    
    // example 1: return an image in image tags
    return '<img src="' . get_post_meta($post->ID, "my_custom_field_image", true) . '" class="custom-image" />';

    // example 2: return just the contents of the custom field
    return get_post_meta($post->ID, "my_custom_field_name", true);

    // example 3: return the result of a shortcode
    return do_shortcode( get_post_meta($post->ID , "my-shortcode" , true) );
}
add_shortcode('insert-custom-field', 'get_my_custom_field');

Usage

The example shows three ways to return the content. Only one should be used.

  • The first example assumes that the contents of the custom field my_custom_field_image is an image URL, so it will return the image url in html <img> tags, so that the image will be displayed as an image, and not the url of that image.
  • The second example retrieves the value of the custom field my_custom_field_name and will return that value. Insert the shortcode [insert-custom-field] in your LoopBuddy layout where you want the comment form to appear.
  • You can even add a shortcode to the custom field, example 3 shows how to return the results of a shortcode my_shortcode and insert that in your layout. The custom field contents should include the shortcode brackets, so [my_shortcode].

Additional documentation

get_post_meta function reference from WordPress Codex


← Back to LoopBuddy Codex Home

Personal tools
Namespaces
Variants
Actions
iThemes Codex
Codex Navigation
Toolbox