Created
September 30, 2016 18:44
-
-
Save zzap/b8e2e3eea0f5020cbca192528d15a555 to your computer and use it in GitHub Desktop.
Check if custom field is populated and echo it or return its value, if any.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Custom meta | |
* | |
* Check if the field is populated and echo it | |
* or return its value, if any. | |
* | |
* @link https://developer.wordpress.org/reference/functions/get_post_meta/ | |
* | |
* @param string $custom_field Unique id for custom field | |
* @param boolean $echo Whether to echo or return the value | |
* @param string $before Custom content before the value | |
* @param string $after Custom content after the value | |
* @return string Returns or echoes value for the custom field | |
*/ | |
function zzap_custom_meta( $custom_field = '', $echo = true, $before = '', $after = '' ) { | |
// Translators: post id, custom field id, return a single value | |
if ( get_post_meta( get_the_ID(), $custom_field, true ) ) { | |
if ( $echo ) { | |
echo $before . get_post_meta( get_the_ID(), $custom_field, true ) . $after; | |
} else { | |
return get_post_meta( get_the_ID(), $custom_field, true ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment