12. Champs Relationnels (Relation, Objet de Publication, Taxonomie) | Field Forge - Champs personnalisés, conçus pour la vitesse
Télécharger Se connecter

12. Champs Relationnels (Relation, Objet de Publication, Taxonomie)

Champs qui créent des connexions entre le contenu. Nécessite une licence PRO.

Relation

Sélecteur de publication multi-sélection qui retourne un tableau de publications.

php
$related = get_field( 'related_articles' );
// return_format = 'object': Returns array of WP_Post objects
// return_format = 'id': Returns array of post IDs, e.g., [10, 25, 33]

if ( $related ) :
    echo '<div class="related-articles">';
    echo '<h3>Related Articles</h3>';
    foreach ( $related as $post ) : setup_postdata( $post ); ?>
        <article class="related-card">
            <?php if ( has_post_thumbnail() ) : ?>
                <?php the_post_thumbnail( 'medium' ); ?>
            <?php endif; ?>
            <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
            <?php the_excerpt(); ?>
        </article>
    <?php endforeach;
    wp_reset_postdata();
    echo '</div>';
endif;

// When return_format = 'id', use get_posts for more control
$related_ids = get_field( 'related_articles' );
if ( $related_ids ) {
    $posts = get_posts( [
        'post__in'       => $related_ids,
        'orderby'        => 'post__in',
        'posts_per_page' => -1,
    ] );
    foreach ( $posts as $p ) {
        echo '<a href="' . get_permalink( $p ) . '">' . esc_html( $p->post_title ) . '</a>';
    }
}

Objet de Publication

Sélectionnez une ou plusieurs publications.

php
$featured = get_field( 'featured_post' );
// return_format = 'object': Returns WP_Post object (or array if multiple)

if ( $featured ) {
    echo '<a href="' . get_permalink( $featured ) . '">';
    echo esc_html( $featured->post_title );
    echo '</a>';
}

// Multiple post objects
$highlights = get_field( 'highlighted_posts' );
if ( $highlights ) {
    foreach ( $highlights as $post ) {
        echo '<div class="highlight">';
        echo '<h4>' . esc_html( $post->post_title ) . '</h4>';
        echo '</div>';
    }
}

Lien de Page

Retourne l’URL permanente d’une publication/page sélectionnée.

php
$link = get_field( 'destination_page' );
// Returns: URL string (permalink of selected page)
echo '<a href="' . esc_url( $link ) . '" class="btn">Go to page</a>';

Taxonomie

Sélectionnez des termes de taxonomie.

php
$term = get_field( 'primary_category' );
// return_format = 'object': Returns WP_Term object

if ( is_object( $term ) ) {
    echo '<a href="' . esc_url( get_term_link( $term ) ) . '">' . esc_html( $term->name ) . '</a>';
}

// Multiple terms
$tags = get_field( 'related_tags' );
if ( $tags ) {
    echo '<div class="tags">';
    foreach ( $tags as $tag ) {
        echo '<a href="' . esc_url( get_term_link( $tag ) ) . '" class="tag">' . esc_html( $tag->name ) . '</a>';
    }
    echo '</div>';
}

Utilisateur

Sélectionnez un ou plusieurs utilisateurs WordPress.

php
$author = get_field( 'guest_author' );
// return_format = 'array': Returns ['ID' => 5, 'display_name' => 'John', 'user_email' => '...']

if ( $author ) {
    echo '<div class="author-card">';
    echo get_avatar( $author['ID'], 64 );
    echo '<span>' . esc_html( $author['display_name'] ) . '</span>';
    echo '</div>';
}

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