Skip to content

Instantly share code, notes, and snippets.

@braddalton
Created October 22, 2015 16:08
Show Gist options
  • Save braddalton/2c829d09d12ba6554d1d to your computer and use it in GitHub Desktop.
Save braddalton/2c829d09d12ba6554d1d to your computer and use it in GitHub Desktop.
genesis_get_custom_field Function to print custom field value in Genesis http://wp.me/p1lTu0-g6V
/**
* Echo data from a post or page custom field.
*
* Echo only the first value of custom field.
*
* Pass in a `printf()` pattern as the second parameter and have that wrap around the value, if the value is not falsy.
*
* @since 0.1.3
*
* @uses genesis_get_custom_field() Return custom field post meta data.
*
* @param string $field Custom field key.
* @param string $output_pattern printf() compatible output pattern.
*/
function genesis_custom_field( $field, $output_pattern = '%s' ) {
if ( $value = genesis_get_custom_field( $field ) )
printf( $output_pattern, $value );
}
/**
* Return custom field post meta data.
*
* Return only the first value of custom field. Return empty string if field is blank or not set.
*
* @since 0.1.3
*
* @param string $field Custom field key.
* @param int $post_id Optional. Post ID to use for Post Meta lookup, defaults to get_the_ID()
*
* @return string|boolean Return value or empty string on failure.
*/
function genesis_get_custom_field( $field, $post_id = null ) {
//* Use get_the_ID() if no $post_id is specified
$post_id = ( null !== $post_id ? $post_id : get_the_ID() );
if ( null === $post_id ) {
return '';
}
$custom_field = get_post_meta( $post_id, $field, true );
if ( ! $custom_field ) {
return '';
}
//* Return custom field, slashes stripped, sanitized if string
return is_array( $custom_field ) ? stripslashes_deep( $custom_field ) : stripslashes( wp_kses_decode_entities( $custom_field ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment