Skip to content

Instantly share code, notes, and snippets.

@ramseyp
Last active December 20, 2015 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramseyp/6194398 to your computer and use it in GitHub Desktop.
Save ramseyp/6194398 to your computer and use it in GitHub Desktop.
Returns first value of WordPress custom field. Runs it though wpautop() if desired. Has option for you to insert text / html before and after the value, allowing you to wrap the value with content.
<?php
/**
* Displays the content of a custom field, along with wrapping code or content.
* @param string $key name, or key, of the custom field.
* @param string $before what should be shown directly before the custom field value.
* @param string $after what is output directly after the custom field value.
* @param boolean $wpautop filters the value through wpautop().
* @return string first value of the custom field, prepended by $before, followed by $after.
*/
function s25_do_post_meta( $key, $before, $after, $wpautop ) {
global $post;
if ( $wpautop ) :
$custom_field = wpautop( get_post_meta($post->ID, $key, true), false );
else :
$custom_field = get_post_meta($post->ID, $key, true);
endif;
if($custom_field) : //if the user set a custom field
echo $before . $custom_field . $after;
else : //else, return
return;
endif;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment