Support

Account

Home Forums Front-end Issues If statement based on field type

Solved

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?

    • Elliot

    • September 11, 2013 at 2:01 pm

    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?

    • Elliot

    • September 11, 2013 at 2:14 pm

    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?

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.