2. get_field() та the_field() | Field Forge - Кастомні поля, створені для швидкості
Завантажити Увійти

2. get_field() та the_field()

Дві найосновніші функції в Field Forge. get_field() отримує значення; the_field() виводить його безпосередньо.

get_field( $selector, $post_id )

Отримати одне значення поля.

Параметри:
ПараметрТипЗа замовчуваннямОпис
$selectorstringНазва поля (наприклад, 'subtitle') або ключ поля (наприклад, 'field_abc123')
$post_idint\string\falsefalseID поста, 'options', 'user_123', 'term_45', 'comment_67' або false для поточного поста в The Loop
Повертає: mixed — значення поля, відформатоване відповідно до налаштування return_format поля. Повертає null, якщо не знайдено. Посилання на формат ID поста:
ФорматПрикладЦіль
falseget_field('title')Поточний пост (вимагає The Loop)
intget_field('title', 42)Конкретний пост за ID
'options'get_field('logo', 'options')Сторінка параметрів (за замовчуванням slug)
'user_N'get_field('bio', 'user_5')Мета користувача для ID користувача 5
'term_N'get_field('color', 'term_12')Мета терміна для ID терміна 12
'comment_N'get_field('rating', 'comment_99')Мета коментаря для ID коментаря 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 )

Вивести значення поля. Еквівалентно echo get_field(...).

Параметри:
ПараметрТипЗа замовчуваннямОпис
$selectorstringНазва поля або ключ поля
$post_idint\string\falsefalseТе ж саме, що й get_field()
Повертає: void (виводить безпосередньо).
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>

Асистент ШІ Forge Онлайн

Привіт! Я асистент ШІ Field Forge. Запитайте мене про будь-що щодо плагіна — налаштування, можливості, усунення несправностей чи розробку.

Щойно
Працює на Forge AI · Переглянути документацію