2. get_field() und the_field() | Field Forge - Benutzerdefinierte Felder, gebaut für Geschwindigkeit
Herunterladen Anmelden

2. get_field() und the_field()

Die beiden grundlegendsten Funktionen in Field Forge. get_field() ruft einen Wert ab; the_field() gibt ihn direkt aus.

get_field( $selector, $post_id )

Ruft einen einzelnen Feldwert ab.

Parameter:
ParameterTypStandardBeschreibung
$selectorstringFeldname (z. B. 'subtitle') oder Feldschlüssel (z. B. 'field_abc123')
$post_idint\string\falsefalsePost-ID, 'options', 'user_123', 'term_45', 'comment_67' oder false für den aktuellen Beitrag in The Loop
Gibt zurück: mixed — der Feldwert formatiert gemäß der return_format Einstellung des Feldes. Gibt null zurück, wenn nicht gefunden. Post-ID-Formatreferenz:
FormatBeispielZiel
falseget_field('title')Aktueller Beitrag (erfordert The Loop)
intget_field('title', 42)Spezifischer Beitrag nach ID
'options'get_field('logo', 'options')Optionsseite (Standard-Slug)
'user_N'get_field('bio', 'user_5')Benutzer-Meta für Benutzer-ID 5
'term_N'get_field('color', 'term_12')Term-Meta für Term-ID 12
'comment_N'get_field('rating', 'comment_99')Kommentar-Meta für Kommentar-ID 99
php
// Current post (inside The Loop)
$title = get_field( 'custom_title' );

// Specific post
$price = get_field( 'product_price', 42 );

// Options page
$logo = get_field( 'site_logo', 'options' );

// User meta
$bio = get_field( 'biography', 'user_' . get_current_user_id() );

// Term meta
$category_color = get_field( 'brand_color', 'term_' . $term_id );
Beispiel für Rückgabeformate nach Feldtyp:
php
// Image field with return_format = 'array' (default)
$image = get_field( 'hero_image', 123 );
// Returns: ['ID' => 55, 'url' => '...', 'width' => 1200, 'height' => 630, 'alt' => '...', 'title' => '...']

// Image field with return_format = 'url'
$image_url = get_field( 'hero_image', 123 );
// Returns: 'https://example.com/wp-content/uploads/2024/hero.jpg'

// Image field with return_format = 'id'
$image_id = get_field( 'hero_image', 123 );
// Returns: 55

// Select field with return_format = 'value'
$status = get_field( 'order_status', 200 );
// Returns: 'processing'

// Select field with return_format = 'label'
$status_label = get_field( 'order_status', 200 );
// Returns: 'Processing'

// Select field with return_format = 'array'
$status_full = get_field( 'order_status', 200 );
// Returns: ['value' => 'processing', 'label' => 'Processing']

// True/false field always returns bool
$is_featured = get_field( 'is_featured', 123 );
// Returns: true or false

// Relationship field with return_format = 'object'
$related = get_field( 'related_posts', 42 );
// Returns: [WP_Post, WP_Post, WP_Post]

// Repeater field returns array of row arrays
$rows = get_field( 'team_members', 42 );
// Returns: [['name' => 'Alice', 'role' => 'Dev'], ['name' => 'Bob', 'role' => 'Design']]

the_field( $selector, $post_id )

Gibt einen Feldwert aus. Entspricht echo get_field(...).

Parameter:
ParameterTypStandardBeschreibung
$selectorstringFeldname oder Feldschlüssel
$post_idint\string\falsefalseDasselbe wie get_field()
Gibt zurück: void (gibt direkt aus).
php
<!-- Current post -->
<h1><?php the_field( 'page_heading' ); ?></h1>

<!-- Link with field values -->
<a href="<?php the_field( 'cta_link' ); ?>">
    <?php the_field( 'cta_text' ); ?>
</a>

<!-- Options page value -->
<footer>
    <p><?php the_field( 'footer_copyright', 'options' ); ?></p>
</footer>

<!-- User profile -->
<div class="author-bio">
    <p><?php the_field( 'biography', 'user_' . get_the_author_meta( 'ID' ) ); ?></p>
</div>

<!-- Specific post -->
<div class="promo-banner">
    <h3><?php the_field( 'promo_headline', 99 ); ?></h3>
    <p><?php the_field( 'promo_description', 99 ); ?></p>
</div>

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