Los campos de grupo agrupan subcampos relacionados en una única unidad nombrada. A diferencia de los repetidores (que tienen múltiples filas), un grupo siempre tiene exactamente un conjunto de valores de subcampo. Los grupos son ideales para bloques de datos estructurados como direcciones, configuraciones de héroe o metadatos SEO.
Accediendo al Grupo como Array
php
// Method 1: use have_rows (recommended)
if ( have_rows( 'address', 42 ) ) :
while ( have_rows( 'address', 42 ) ) : the_row();
$street = get_sub_field( 'street' );
$city = get_sub_field( 'city' );
$state = get_sub_field( 'state' );
$zip = get_sub_field( 'zip' );
endwhile;
endif;
echo '<address>';
echo esc_html( $street ) . '<br>';
echo esc_html( $city ) . ', ' . esc_html( $state ) . ' ' . esc_html( $zip );
echo '</address>';
// Method 2: Direct get_field returns the whole group as an array
$address = get_field( 'address', 42 );
if ( $address ) {
echo esc_html( $address['street'] ) . '<br>';
echo esc_html( $address['city'] ) . ', ' . esc_html( $address['state'] );
}Grupo para Configuraciones de Diseño/Estilo
php
<?php
if ( have_rows( 'hero_settings' ) ) :
while ( have_rows( 'hero_settings' ) ) : the_row();
$bg_color = get_sub_field( 'background_color' );
$text_color = get_sub_field( 'text_color' );
$overlay = get_sub_field( 'overlay_opacity' );
$bg_image = get_sub_field( 'background_image' );
endwhile;
endif;
?>
<section class="hero"
style="background-color: <?php echo esc_attr( $bg_color ); ?>;
color: <?php echo esc_attr( $text_color ); ?>;
<?php if ( $bg_image ) : ?>
background-image: url(<?php echo esc_url( $bg_image['url'] ); ?>);
<?php endif; ?>">
<div class="hero-content">
<h1><?php the_field( 'hero_heading' ); ?></h1>
</div>
</section>Acceso Programático al Grupo
php
// Update group sub-fields
update_field( 'address', [
'street' => '123 Main St',
'city' => 'Portland',
'state' => 'OR',
'zip' => '97201',
], 42 );
// Read and modify a group
$address = get_field( 'address', 42 );
$address['zip'] = '97202';
update_field( 'address', $address, 42 );—