12. Campos Relacionales (Relación, Objeto de Publicación, Taxonomía) | Field Forge - Campos personalizados, diseñados para la velocidad
Descargar Iniciar sesión

12. Campos Relacionales (Relación, Objeto de Publicación, Taxonomía)

Campos que crean conexiones entre contenidos. Requiere licencia PRO.

Relación

Selector de publicaciones de selección múltiple que devuelve un array de publicaciones.

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>';
    }
}

Objeto de Publicación

Selecciona una o varias publicaciones.

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>';
    }
}

Enlace de Página

Devuelve la URL permanente de una publicación/página seleccionada.

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

Taxonomía

Selecciona términos de taxonomía.

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>';
}

Usuario

Selecciona uno o más usuarios de 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>';
}

Asistente de IA de Forge En línea

¡Hola! Soy el asistente de IA de Field Forge. Pregúntame lo que quieras sobre el plugin — configuración, funciones, resolución de problemas o desarrollo.

Ahora mismo
Con la tecnología de Forge AI · Explorar documentación