Skip to content
Documentation menu

Flexible content

Flexible Content is part of Ultivo Toolkit Pro. In the free version the field type is not available; a field group that already uses one keeps its content untouched and shows a locked field until Pro is active. See pricing.

Layouts#

A Flexible Content field defines one or more layouts: say hero, text_block and gallery, each with its own label, name and sub-fields. Editors then build the page as an ordered list of layout rows: pick a layout from the add button, fill in its fields, drag to reorder. Minimum / Maximum Layouts bound the number of rows.

Think of it as a repeater where every row chooses its own set of sub-fields.

Looping layouts#

In the classic loop, ultivo_get_row_layout() returns the current row's layout name:

if ( ultivo_have_rows( 'blocks' ) ) :
    while ( ultivo_have_rows( 'blocks' ) ) : ultivo_the_row();
        if ( 'hero' === ultivo_get_row_layout() ) {
            $title = ultivo_get_sub_field( 'title' );
        } elseif ( 'text_block' === ultivo_get_row_layout() ) {
            $content = ultivo_get_sub_field( 'content' );
        }
    endwhile;
endif;

The Template API asks the row object instead:

foreach ( ultivo_rows( 'blocks' ) as $block ) {
    if ( 'hero' === $block->layout() ) {
        echo esc_html( $block['title'] );
    }
}

Rendering with a template file per layout#

For anything beyond a couple of layouts, hand the loop to render(): it loads one theme file per row, named after the row's layout:

ultivo_rows( 'blocks' )->render( 'template-parts/blocks' );

This includes template-parts/blocks/{layout}.php for each row: hero.php, text_block.php, and so on. Inside the file, the current row is available as $row:

<h2><?php echo esc_html( $row['title'] ); ?></h2>

Rows whose layout has no matching template file are skipped, so removing a layout's file never causes a fatal error. Adding a new layout is then a two-step job: add it in the builder, drop a matching file in the directory.