3. get_fields() et get_field_objects() | Field Forge - Champs personnalisés, conçus pour la vitesse
Télécharger Se connecter

3. get_fields() et get_field_objects()

Fonctions de récupération en masse pour obtenir toutes les valeurs de champ ou toutes les définitions de champ pour une publication à la fois.

get_fields( $post_id )

Récupérer toutes les valeurs de champ pour une publication sous forme de tableau associatif.

Paramètres :
ParamètreTypePar défautDescription
$post_idint\string\falsefalseID de publication, 'options', 'user_N', ou false pour la publication actuelle
Renvoie : array — tableau associatif de field_name => value.
php
// Get all fields for the current post
$fields = get_fields();
// Returns: ['subtitle' => 'Hello', 'price' => '29.99', 'is_featured' => true, ...]

// Get all fields for a specific post
$product_data = get_fields( 42 );

// Get all option values
$options = get_fields( 'options' );
// Returns: ['site_logo' => [...], 'site_phone' => '+1-555-0123', ...]

// Use in a REST API response or JSON output
wp_send_json_success( get_fields( $post_id ) );

// Build a structured data array for JSON-LD
$product = get_fields( $post_id );
$json_ld = [
    '@context' => 'https://schema.org',
    '@type'    => 'Product',
    'name'     => get_the_title( $post_id ),
    'offers'   => [
        '@type'         => 'Offer',
        'price'         => $product['price'] ?? '0',
        'priceCurrency' => 'USD',
    ],
];
echo '<script type="application/ld+json">' . wp_json_encode( $json_ld ) . '</script>';

get_field_objects( $post_id )

Obtenir toutes les définitions de champ (avec valeurs) pour une publication.

Paramètres :
ParamètreTypePar défautDescription
$post_idint\string\falsefalseID de publication, 'options', ou false pour la publication actuelle
Renvoie : array — tableau associatif de field_name => field_object.
php
$objects = get_field_objects( 42 );

// Render all fields automatically based on type
foreach ( $objects as $name => $field ) {
    if ( $field['type'] === 'image' && $field['value'] ) {
        echo '<figure>';
        echo '<img src="' . esc_url( $field['value']['url'] ) . '" alt="' . esc_attr( $field['value']['alt'] ) . '">';
        echo '<figcaption>' . esc_html( $field['label'] ) . '</figcaption>';
        echo '</figure>';
    } elseif ( $field['type'] === 'true_false' ) {
        echo '<p><strong>' . esc_html( $field['label'] ) . ':</strong> ';
        echo $field['value'] ? 'Yes' : 'No';
        echo '</p>';
    } elseif ( $field['value'] ) {
        echo '<p><strong>' . esc_html( $field['label'] ) . ':</strong> ';
        echo esc_html( $field['value'] ) . '</p>';
    }
}

// Export all field definitions for documentation
$all_objects = get_field_objects( 42 );
$schema = [];
foreach ( $all_objects as $name => $obj ) {
    $schema[] = [
        'name'     => $obj['name'],
        'type'     => $obj['type'],
        'label'    => $obj['label'],
        'required' => $obj['required'] ?? false,
    ];
}

Assistant IA Forge En ligne

Bonjour ! Je suis l'assistant IA Field Forge. Posez-moi n'importe quelle question sur le plugin — configuration, fonctionnalités, dépannage ou développement.

À l'instant
Propulsé par Forge IA · Parcourir la documentation