Home › Forums › Front-end Issues › If statement based on field type
Random question here.
Is there a way to use an if statement based on field type?
Example: say i want to call an array like below for only radio buttons, but not text fields??
$map = array(
'yes' => '<i class="icon-ok"></i>',
'no' => '<i class="icon-remove"></i>'
);
So would something like this work (assuming the correct calling of a field type:
if ($fields = [select]) {
$map = array(
'yes' => '<i class="icon-ok"></i>',
'no' => '<i class="icon-remove"></i>'
);
} else {
};
Figured it out!!
I took a chance and made an if statement based on what I thought was the radio button type (turned out to be ‘choices’) and it actually worked.
Here’s the code i used. So this calls all fields within a group, takes out any that doesn’t have a value, then puts an icon instead of a ‘yes or ‘no’ for the radio button, yet keeps all other fields loading like they normally do:
$group_ID = 6817;
$fields = array();
$fields = apply_filters('acf/field_group/get_fields', $fields, $group_ID);
if( $fields )
{
foreach( $fields as $field_name => $field )
{
$value = get_field( $field['name'] );
if ($field['choices']){
$map = array(
'yes' => '<i class="icon-ok"></i>',
'no' => '<i class="icon-remove"></i>'
);
$value = $map[ $value ];
} else {
}
if( $value ) {
echo '<li><b> ' . $field['label'] . '</b> ' . $value . '</li>';
}
}
Now to find an if statement to remove any radio buttons that have ‘no’ as their selection…
Would it be possible to use an if statement like:
if( $value == 'yes' ) {
//post the value
??
Or would it need to call an array first then use a $yes as the code?
Hi @aaronrobb
Perhaps instead of if ($field['choices']){
, you could look at the type as suggested in your topic title like so:
if ($field['type'] == 'radio'){
As for the yes / no question, I’m completely lost as to what you are trying to achieve. Would you mind explaining the result you want?
Thanks
E
Definitely:
I have a number of radio button fields. They all have either ‘yes’ or ‘no’ as the value. Is there a way to say ‘if value = yes then *do whatever*’, meaning if the value is ‘no’ i dont want it to show at all.
DOes that make sense?
Hi @aaronrobb
of course, you could achieve this with an if statement like so:
if( get_field('field_name') == 'yes' )
{
// do this
}
else
{
// do that
}
Does that help?
The topic ‘If statement based on field type’ is closed to new replies.
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.