Skip to content

Instantly share code, notes, and snippets.

@maxxscho
Created January 22, 2015 13:45
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 maxxscho/c53987bc2972388e241e to your computer and use it in GitHub Desktop.
Save maxxscho/c53987bc2972388e241e to your computer and use it in GitHub Desktop.
Echo a field of Advanced Custom Fields, if it is present. You may prepend and append it with custom strings (HTML ;) )
<?php
/**
* Echo an ACF Field if it is present
* @param bool|int $field Name of the field
* @param string $before prepend the field with this string. HTML for example
* @param string $after append the field with this string. HTML for example
* @param bool|int $post_id
*
* @return bool|void
* @author Markus Schober
*/
function the_field_if($field = false, $before = '', $after = '', $post_id = false)
{
if ( get_field($field, $post_id) ) {
echo $before . get_field($field, $post_id) . $after;
}
else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment