Support

Account

Home Forums Backend Issues (wp-admin) Multiple values for checkbox choices Reply To: Multiple values for checkbox choices

  • I would probably do something more complex with less code if you have many of these, or you plan on adding many more in the future, so that I can change the values and not look at the template again. I just did something recently where I do something similar. What I would do is have a function that returns an array of values.

    
    function my_tool_choices() {
      $tools = array(
        'wordpress' => array(
          'label' => 'WordPress',
          'icon' => 'fa fa-wordpress',
          'url' => 'http://ws1/'
        ),
       'plugin a' => array(
          'label' => 'Pludin A',
          'icon' => 'fa fa-apple',
          'url' => 'http://ws2/'
        )
      )
      return $tools;
    }
    

    You can then add new tools to this as you need them. Then I would create an acf/load_field filter to populate the choices from this array. https://www.advancedcustomfields.com/resources/acf-load_field/

    
    add_filter('acf/load_field/name=my_tools', 'load_acf_tools_choices');
    function load_acf_tools_choices($field) {
      $tools = my_tool_choices();
      $choices = array();
      foreach ($tools as $value => $tool) {
        $choices[$value] = $tool['label'];
      }
      $field['choices'] = $choices;
      return $field;
    }
    

    Then in the template I would do something like this

    
    $tools = my_tool_choices();
    $values = get_field('my_tools');
    foreach ($values as $value) {
      ?><a href="<?php 
          echo $tools[$value]['url']; ?>" target="_blank"><i class="<?php 
          echo $tools[$value]['icon']; ?>"></i><?php 
          echo $tools[$value]['label']; ?></a><?php 
    }