Radio Button

Overview

The Radio button field creates a list of select-able inputs.

Screenshots

Changelog

  • Return Format setting added in version 5.4.0
  • Allow Null setting added in version 5.3.8
  • Other & Save Other settings added in version 4.1.7

Settings

Name Description
Choices Each choice is entered on a new line (eg. ‘Red’). For more control over the value and label, you may use a ‘ : ‘ character to specify both (eg. ‘red : Red’)
Allow Null By default it is not possible to ‘un-select’ an input. This setting allows you to do so using JS
Other Adds a text input allowing for a custom value to be entered
Save Other Allows the custom value to be appended to the field’s choices
Default Value Specify the default value selected when first editing the field’s value. Enter only value (not label)
Layout Changes the layout style of inputs from Vertical to Horizontal
Return Format Change the value format returned by the get_field() and similar functions.

Notes

Save Custom

If using the local JSON feature, any custom values saved to the fieldโ€™s choices will not appear on page reload. This is because the JSON file will not be updated and will override any field settings found in the DB.

Template usage

Basic

This example shows how to load and display a single selected value.

<p>Color: <?php the_field('color'); ?></p>

Display value and label

This example shows how to load a selected value and label without using the ‘Format value’ setting.

<?php

// vars
$field = get_field_object('color');
$value = $field['value'];
$label = $field['choices'][ $value ];

?>
<p>Color: <span class="color-<?php echo $value; ?>"><?php echo $label; ?></span></p>

Format value setting

This example shows how to load a selected value and label using the ‘Format value’ setting (set to ‘Both’).

<?php

// vars
$color = get_field('color');

?>
<p>Color: <span class="color-<?php echo $color['value']; ?>"><?php echo $color['label']; ?></span></p>

Conditional

This example shows how to use a selected value to conditionally perform a task.

<?php if( get_field('color') == 'red' ): ?>
	<p>Selected the Red choice!</p>
<?php endif; ?>

Query posts

This example shows how to query posts that have the value ‘red’ selected.

<?php

$posts = get_posts(array(
    'meta_query' => array(
        array(
            'key' => 'color', // name of custom field
            'value' => 'red',
        )
    )
));

if( $posts ) {
    //...
}

?>