17. Гнучкий вміст | Field Forge - Кастомні поля, створені для швидкості
Завантажити Увійти

17. Гнучкий вміст

Поля гнучкого вмісту дозволяють редакторам вибирати з попередньо визначених макетів для створення динамічних сторінок. Кожен макет має свій набір підполів. Вимагає ліцензії PRO.

Основне перемикання макетів

php
<?php if ( have_rows( 'page_sections' ) ) : ?>
    <?php while ( have_rows( 'page_sections' ) ) : the_row(); ?>
        <?php if ( get_row_layout() === 'hero' ) : ?>
            <section class="section-hero">
                <h1><?php the_sub_field( 'heading' ); ?></h1>
                <p><?php the_sub_field( 'subheading' ); ?></p>
            </section>
        <?php elseif ( get_row_layout() === 'text_block' ) : ?>
            <section class="section-text">
                <?php the_sub_field( 'content' ); ?>
            </section>
        <?php elseif ( get_row_layout() === 'image_gallery' ) : ?>
            <section class="section-gallery">
                <?php $images = get_sub_field( 'images' ); ?>
                <?php if ( $images ) : ?>
                    <div class="gallery-grid">
                        <?php foreach ( $images as $img ) : ?>
                            <img src="<?php echo esc_url( $img['url'] ); ?>" alt="<?php echo esc_attr( $img['alt'] ); ?>">
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </section>
        <?php endif; ?>
    <?php endwhile; ?>
<?php endif; ?>

Шаблон частини

Більш чистий підхід з використанням get_template_part():

php
<?php
// page-builder.php
if ( have_rows( 'sections' ) ) :
    while ( have_rows( 'sections' ) ) : the_row();
        $layout = get_row_layout();
        get_template_part( 'template-parts/sections/' . str_replace( '_', '-', $layout ) );
    endwhile;
endif;
?>

Кожен файл шаблону секції безпосередньо отримує доступ до підполів:

php
<?php
// template-parts/sections/hero.php
$heading = get_sub_field( 'heading' );
$image   = get_sub_field( 'background_image' );
?>
<section class="hero" <?php if ( $image ) : ?>style="background-image: url(<?php echo esc_url( $image['url'] ); ?>);"<?php endif; ?>>
    <h1><?php echo esc_html( $heading ); ?></h1>
    <p><?php the_sub_field( 'subheading' ); ?></p>
</section>

Програмне створення гнучкого вмісту

php
$sections = [
    [
        'acf_fc_layout' => 'hero',
        'heading'        => 'Welcome to Our Site',
        'subheading'     => 'We build amazing things.',
        'cta_text'       => 'Learn More',
        'cta_link'       => '/about',
    ],
    [
        'acf_fc_layout' => 'text_block',
        'content'        => '<p>Our story begins with a simple idea...</p>',
    ],
];

$field_def = FIELDFORGE_Field_Groups::instance()->find_field( 'page_builder', $post_id );
FIELDFORGE_Field_Values::instance()->save_compound_field(
    'page_builder',
    $sections,
    $post_id,
    $field_def
);

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

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

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