11. Choice Fields (Select, Checkbox, Radio) | Field Forge - Custom Fields, Built for Speed
Download Log in

11. Choice Fields (Select, Checkbox, Radio)

Developer Guide

Fields that present a set of predefined options for the editor to choose from.

Select

Single or multiple selection from a list of choices.

php
// return_format = 'value' (default)
$value = get_field( 'category' );
// Returns: 'electronics'

// return_format = 'label'
$label = get_field( 'category' );
// Returns: 'Electronics'

// return_format = 'array'
$full = get_field( 'category' );
// Returns: ['value' => 'electronics', 'label' => 'Electronics']

// If multiple = true, returns array of values
$categories = get_field( 'categories' );
// Returns: ['electronics', 'accessories']

// Use select value for conditional styling
$priority = get_field( 'priority' );
$colors = [
    'low'    => '#28a745',
    'medium' => '#ffc107',
    'high'   => '#dc3545',
];
$color = $colors[ $priority ] ?? '#6c757d';
echo '<span class="priority-badge" style="background: ' . esc_attr( $color ) . ';">' . esc_html( $priority ) . '</span>';

Checkbox

Returns an array of selected values.

php
$values = get_field( 'amenities' );
// Returns: array of selected values, e.g., ['wifi', 'parking', 'pool']

if ( $values ) : ?>
    <ul class="amenities">
        <?php foreach ( $values as $amenity ) : ?>
            <li class="amenity amenity--<?php echo esc_attr( $amenity ); ?>">
                <?php echo esc_html( $amenity ); ?>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif;

// Check if a specific value is selected
$amenities = get_field( 'amenities' );
if ( $amenities && in_array( 'wifi', $amenities, true ) ) {
    echo '<span class="has-wifi">Free WiFi Available</span>';
}

Radio Button

Single-choice selection.

php
$value = get_field( 'layout_style' );
// Returns: 'grid' (single selected value)

echo '<div class="layout-' . esc_attr( $value ) . '">';

// Use radio value to load different templates
$layout = get_field( 'content_layout' );
get_template_part( 'template-parts/layout', $layout );

True / False

Boolean toggle that returns true or false.

php
$is_featured = get_field( 'is_featured' );
// Returns: true or false (boolean)

if ( $is_featured ) {
    echo '<span class="featured-badge">Featured</span>';
}

// Use in conditional classes
$class = get_field( 'full_width' ) ? 'container-fluid' : 'container';
echo '<div class="' . esc_attr( $class ) . '">';

Button Group

Visual button-style selector, like radio but styled as a button group.

php
$alignment = get_field( 'text_align' );
// Returns: 'left', 'center', or 'right' (single value, like radio)

echo '<div style="text-align: ' . esc_attr( $alignment ) . ';">Content</div>';

Forge AI Assistant Online

Hi! I'm the Field Forge AI assistant. Ask me anything about the plugin — setup, features, troubleshooting, or development.

Just now
Powered by Forge AI · Browse docs