Support

Account

Home Forums Front-end Issues exclude field on fields array

Solved

exclude field on fields array

  • Hello,

    I use this code that I found on the forum to display all my custom fields of group 30109

    However I would like to know if I can exclude custom fields?

    Example I would like to display everything except

    – amount
    – type
    – model

    Here is the code

    <?php
    $group_ID = 30109;
    $fields = array();
    $fields = apply_filters('acf/field_group/get_fields', $fields, $group_ID);
    
    if( $fields )
    {
    echo '<div class="col-sm-6">';
    echo '<h3>Description:</h3>';
        echo '<ul class="list-unstyled">';
      foreach( $fields as $field_name => $field )
      {
        $value = get_field( $field['name'] );
        if ($field['choices']){
          $map = array(
           'yes' => '<i class="icon-ok"></i>'       
           );
          $value = $map[ $value ];
        } else {
        }
        if( $value && $value != 'no') {
          echo '<li>' . $field['label'] . '&nbsp;' . $value . '</li>';
      }
    }
      echo '</ul>';
    echo '</div>';
    }
    ?>

    Thank you

  • Hello,

    Is it possible to exclude with my code present at the top?

    thank you

  • You would need to check them against something, like an array of field keys, for example:

    
    // list of field keys to not show
    $exclude = array('field_123', 'field_234', 'field_456');
    
    // your loop
    foreach( $fields as $field_name => $field ) {
      if (in_array($field['key'], $exclude) {
        // one of the fields to exclude
        // skip it
        continue;
      }
      // ..... the rest of your loop here ....
    
  • Hello and thank you for your answer,
    I tried this but I think I did wrong the code,
    Can you see when you have time?
    thank you

    <?php
    $group_ID = 30109;
    $fields = array();
    $fields = apply_filters('acf/field_group/get_fields', $fields, $group_ID);
    
    $exclude = array('field_5919f45752917', 'field_5919f46252918', 'field_5919f46a52919');
    
    if( $fields )
    {
    echo '<div class="col-sm-6">';
    echo '<h3>Description:</h3>';
        echo '<ul class="list-unstyled">';
      foreach( $fields as $field_name => $field ) {
      if (in_array($field['key'], $exclude) {
      {
        $value = get_field( $field['name'] );
        if ($field['choices']){
          $map = array(
           'yes' => '<i class="icon-ok"></i>'       
           );
          $value = $map[ $value ];
        } else {
        }
        if( $value && $value != 'no') {
          echo '<li>' . $field['label'] . '&nbsp;' . $value . '</li>';
      }
        continue;
      }
    }
      echo '</ul>';
    echo '</div>';
    }
    ?>
  • 
    <?php
      $group_ID = 30109;
      $fields = array();
      $fields = apply_filters('acf/field_group/get_fields', $fields, $group_ID);
    
      $exclude = array('field_5919f45752917', 'field_5919f46252918', 'field_5919f46a52919');
    
      if ($fields) {
        echo '<div class="col-sm-6">';
        echo '<h3>Description:</h3>';
        echo '<ul class="list-unstyled">';
        foreach ($fields as $field_name => $field) {
          if (in_array($field['key'], $exclude)) {
            continue;
          }
          $value = get_field( $field['name'] );
          if ($field['choices']){
            $map = array(
             'yes' => '<i class="icon-ok"></i>'       
             );
            $value = $map[ $value ];
          } else {
          }
          if ( $value && $value != 'no') {
            echo '<li>' . $field['label'] . '&nbsp;' . $value . '</li>';
           }
        } // end foreach
        echo '</ul>';
        echo '</div>';
      } // end if fields
    ?>
    
  • Thank you very much for your help,

    it works very well now :-),

    good evening

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

The topic ‘exclude field on fields array’ is closed to new replies.