Home › Forums › General Issues › If a checkbox value is checked › Reply To: If a checkbox value is checked
Here was a simple solution that I came up with for a single checkbox. My use is to declare a title to be echoed as an <h2> or <h3> tag in the html. I post this for anyone else looking for an easy way to use a single checkbox.
if($h3 = get_sub_field('h3_option')){
echo "<h3>{$title}</h3>";
} else {
echo "<h2>{$title}</h2>";
}
I tried the “basic logic” option in on this page: https://www.advancedcustomfields.com/resources/checkbox/
<?php
$selected = get_field('field_name');
if( in_array('red', $selected) ) {
echo 'Red was selected!';
}
?>
But I found it unnecessary for my single selection.
Do note the “get_field” and “get_sub_field” difference in the 2 examples.
I am using sub_field because we are using the flex content. Big difference for those just getting to know this plugin.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.