12. Связанные поля (Отношение, Объект поста, Таксономия) | Field Forge - Произвольные поля, созданные для скорости
Скачать Войти

12. Связанные поля (Отношение, Объект поста, Таксономия)

Поля, которые создают связи между контентом. Требуется лицензия PRO.

Отношение

Множественный выбор постов, который возвращает массив постов.

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

Объект поста

Выберите один или несколько постов.

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

Ссылка на страницу

Возвращает постоянный URL выбранного поста/страницы.

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

Таксономия

Выберите термины таксономии.

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

Пользователь

Выберите одного или нескольких пользователей 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>';
}

ИИ-ассистент Forge Онлайн

Привет! Я ИИ-ассистент Field Forge. Спрашивайте меня о чём угодно по плагину — настройка, возможности, устранение неполадок или разработка.

Только что
На базе Forge AI · Просмотр документации