16. Gruppenfelder | Field Forge - Benutzerdefinierte Felder, gebaut für Geschwindigkeit
Herunterladen Anmelden

16. Gruppenfelder

Gruppenfelder bündeln verwandte Sub-Felder in einer einzigen benannten Einheit. Im Gegensatz zu Wiederholungen (die mehrere Zeilen haben) hat eine Gruppe immer genau einen Satz von Sub-Feldwerten. Gruppen sind ideal für strukturierte Datenblöcke wie Adressen, Hero-Einstellungen oder SEO-Meta.

Zugriff auf Gruppe als 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'] );
}

Gruppe für Layout-/Stileinstellungen

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>

Programmatischer Gruppen-Zugriff

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 );

Forge KI-Assistent Online

Hallo! Ich bin der Field Forge KI-Assistent. Fragen Sie mich alles über das Plugin — Einrichtung, Funktionen, Fehlerbehebung oder Entwicklung.

Gerade eben
Unterstützt von Forge KI · Dokumentation durchsuchen