Skip to content

Instantly share code, notes, and snippets.

@herewithme
Last active February 14, 2018 07:58
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save herewithme/c7c6b247715af1795d90fd427c2d52a5 to your computer and use it in GitHub Desktop.
Save herewithme/c7c6b247715af1795d90fd427c2d52a5 to your computer and use it in GitHub Desktop.
ACF template helper - Checks if at least one field is completed by BO for display or not a block
<?php
/**
* This function checks if at least one field is completed by BO
*
* @param array|string $fields the list of ACF fields to test, an array or a string of text separated by a comma
* @param mixed $object the post_id of which the value is saved against
* @param bool $is_sub_field
*
* @return bool
*/
function is_one_acf_field_filled( $fields, $object = false, $is_sub_field = false ) {
if ( empty( $fields ) ) {
return false;
}
if ( is_string( $fields ) ) {
$fields = explode( ',', $fields );
$fields = array_map( 'trim', $fields );
$fields = array_filter( $fields );
}
if ( empty( $fields ) ) {
return false;
}
foreach ( $fields as $field ) {
if ( true === $is_sub_field ) {
$value = get_sub_field( $field );
} else {
$value = get_field( $field, $object );
}
if ( ! empty( $value ) ) {
return true;
}
}
return false;
}
<?php if ( is_one_acf_field_filled( 'name, mail, phone, fax' ) ) : ?>
<div class="contact-block">
<!-- Show only this DIV if ONE of this field is filled on backend -->
<?php the_field( 'name' ); ?>
<?php the_field( 'mail' ); ?>
<?php the_field( 'phone' ); ?>
<?php the_field( 'fax' ); ?>
</div>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment