name = 'acfe_taxonomies'; $this->label = __('Taxonomies', 'acfe'); $this->category = 'relational'; $this->defaults = array( 'field_type' => 'checkbox', 'return_format' => 'name', ); parent::__construct(); } function render_field($field){ // force value to array $field['value'] = acf_get_array($field['value']); if( $field['field_type'] == 'select' ) { $this->render_field_select( $field ); } elseif( $field['field_type'] == 'radio' ) { $this->render_field_checkbox( $field ); } elseif( $field['field_type'] == 'checkbox' ) { $this->render_field_checkbox( $field ); } } function render_field_select($field){ // Change Field into a select $field['type'] = 'select'; $field['ui'] = 0; $field['ajax'] = 0; $field['choices'] = get_taxonomies(array( 'public' => true, 'show_ui' => true ), 'names'); acf_render_field($field); } function render_field_checkbox($field){ acf_hidden_input(array( 'type' => 'hidden', 'name' => $field['name'], )); if($field['field_type'] == 'checkbox') $field['name'] .= '[]'; $taxonomies = get_taxonomies(array( 'public' => true, 'show_ui' => true ), 'objects'); ?>
__('Appearance','acf'), 'instructions' => __('Select the appearance of this field', 'acf'), 'type' => 'select', 'name' => 'field_type', 'optgroup' => true, 'choices' => array( 'checkbox' => __('Checkbox', 'acf'), 'radio' => __('Radio Buttons', 'acf'), 'select' => _x('Select', 'noun', 'acf') ) )); // return_format acf_render_field_setting( $field, array( 'label' => __('Return Value', 'acf'), 'instructions' => '', 'type' => 'radio', 'name' => 'return_format', 'choices' => array( 'object' => __("Taxonomy Object", 'acfe'), 'name' => __("Taxonomy Name", 'acfe') ), 'layout' => 'horizontal', )); } function format_value($value, $post_id, $field){ if(empty($value)) return false; // force value to array $value = acf_get_array($value); // format = name if($field['return_format'] == 'name'){ if($field['field_type'] == 'select' || $field['field_type'] == 'radio') return array_shift($value); return $value; } // format = object elseif($field['return_format'] == 'object'){ $taxonomies = array(); foreach($value as $taxonomy){ $taxonomies[] = get_taxonomy($taxonomy); } if($field['field_type'] == 'select' || $field['field_type'] == 'radio') return array_shift($taxonomies); return $taxonomies; } // return return $value; } } new acfe_field_taxonomies();