True / False

Overview

The True / False field allows you to select a value that is either 1 or 0.

Screenshots

Changelog

  • ‘UI’ settings added in version 5.5.0

Settings

Name Description
Message This text is displayed alongside the true false input
Default Value Sets the default state (on / off) when adding a new post
Stylized UI Changes the default checkbox input into a stylized toggle switch. Added in v5.5.0
On Text Text displayed within the stylized toggle switch. Defaults to ‘Yes’. HTML may be entered for icons or custom markup. Added in v5.5.0
Off Text Text displayed within the stylized toggle switch. Defaults to ‘No’. HTML may be entered for icons or custom markup. Added in v5.5.0

Template usage

The True / False field will return a Boolean value of either true or false. The value is saved to the DB as ‘1’ or ‘0’.

Basic conditional

This example shows how to use a value to conditionally do a task.

<?php if( get_field('enable_sidebar') ): ?>
	
	<?php // do something ?>
	
<?php endif; ?>

Query posts

This example shows how to query posts that have a true value

<?php 

$posts = get_posts(array(
	'meta_query' => array(
		array(
			'key' => 'enable_sidebar',
			'compare' => '==',
			'value' => '1'
		)
	)
));

if( $posts ): ?>
	
	<ul>
		
	<?php foreach( $posts as $post ): 
		
		setup_postdata( $post )
		
		?>
		<li>
			<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
		</li>
	
	<?php endforeach; ?>
	
	</ul>
	
	<?php wp_reset_postdata(); ?>

<?php endif; ?>

Save