Documentation

Checkbox

Overview

The checkbox field creates a list of tick-able options. This field is useful to add categories or other information to your post or page.

Creating a checkbox field

The checkbox field contains options to customize your field:

  • Choices: You can populate the tick-able list with choices. Each choice is entered on a new line. Each choice requires a value and a label which are broken by a ‘:’. If no ‘:’ is found, the field will use the Label as the Value.

Edit screen

Template usage

The checkbox field will return an array of selected choices. Either use the get_field() function to obtain this array, or use the_field() to simply ouput a coma separated list.

Basic output

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

Custom output

<?php 

$field = get_field_object('field_name');
$value = $field['value'];
$choices = $field['choices'];

if( $value ): ?>
<ul>
	<?php foreach( $value as $v ): ?>
	<li>
		<?php echo $choices[ $v ]; ?>
	</li>
	<?php endforeach; ?>
</ul>
<?php endif; ?>

Basic logic

<?php 

$selected = get_field('field_name');

if( in_array('red', $selected) ) {
	
	echo 'Red was selected!';
	
}

?>

Query posts

<?php

$posts = get_posts(array(
    'meta_query' => array(
        array(
            'key' => 'field_name', // name of custom field
            'value' => '"red"', // matches exactly "red"
            'compare' => 'LIKE'
        )
    )
));

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

?>