Last active
July 24, 2024 17:59
-
-
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
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 | |
/** | |
* 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; | |
} |
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 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