Les deux fonctions les plus fondamentales dans Field Forge. get_field() récupère une valeur ; the_field() l’affiche directement.
get_field( $selector, $post_id )
Récupérer une seule valeur de champ.
Paramètres :| Paramètre | Type | Par défaut | Description | ||
|---|---|---|---|---|---|
$selector | string | — | Nom du champ (par exemple, 'subtitle') ou clé du champ (par exemple, 'field_abc123') | ||
$post_id | int\ | string\ | false | false | ID de publication, 'options', 'user_123', 'term_45', 'comment_67', ou false pour la publication actuelle dans The Loop |
mixed — la valeur du champ formatée selon le paramètre de return_format du champ. Renvoie null si non trouvé.
Référence de format d’ID de publication :
| Format | Exemple | Cible |
|---|---|---|
false | get_field('title') | Publication actuelle (nécessite The Loop) |
int | get_field('title', 42) | Publication spécifique par ID |
'options' | get_field('logo', 'options') | Page d’options (slug par défaut) |
'user_N' | get_field('bio', 'user_5') | Méta utilisateur pour l’ID utilisateur 5 |
'term_N' | get_field('color', 'term_12') | Méta terme pour l’ID de terme 12 |
'comment_N' | get_field('rating', 'comment_99') | Méta commentaire pour l’ID de commentaire 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 );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 )
Affiche une valeur de champ. Équivalent à echo get_field(...).
| Paramètre | Type | Par défaut | Description | ||
|---|---|---|---|---|---|
$selector | string | — | Nom du champ ou clé du champ | ||
$post_id | int\ | string\ | false | false | Même que get_field() |
void (affiche directement).
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>—