How to add content based on a custom field to your posts
October 26, 2011 – 9:26 pm |In your theme files, you may want to add some functionality that shows after your post content if and only if you have a specific custom field set.
This is actually pretty easy to do, just add this code to your functions.php file and make sure to change the name of your custom field key!
-
function sk_single_post_custom_field() {
-
//The key of your custom field
-
$customFieldKey = 'THE CUSTOM FIELD KEY NAME GOES HERE';
-
-
global $post;
-
-
//only show this if we are on the post page
-
if (is_single()) {
-
//gets the custom field from the post object
-
$customFieldData = get_post_meta($post->ID, $customFieldKey, true);
-
if (!is_null($customFieldData) && str_len($customFieldData) > 0) {
-
//output your custom field data here — you can wrap this output in whatever HTML you would like.
-
echo $customFieldData;
-
}
-
}
-
}
For non Thesis (i.e. regular or custom build) themes:
-
//if you are using a custom built, or non thesis theme use this functionality
-
add_filter('the_content', 'sk_single_post_custom_field');
For thesis based themes:
-
//if you are using Thesis – use this to add it into the theme
-
add_action('thesis_hook_after_post', 'single_post_custom_field');

