'wordpress CMB2 get field type
I'm using CMB2 and I need to get the field type for each field
My Backend Code
protected function attach_fields_to_post($fields) {
$options = new_cmb2_box( array(
'id' => $this->plugin_name .'_post_custom_fields',
'title' => __('Post Custom Fields'),
'object_types' => array( 'post' ),
'show_on' => ['id' => $_GET['post']]
) );
if(!empty($fields)) {
$optionsTypes = ['multicheck', 'radio', 'select'];
foreach ($fields as $field) {
$type = $field[$this->plugin_name . '_Field_Type'];
$name = $field[$this->plugin_name . '_Field_Name'];
$optionFields = [
'id' => $this->prefix . str_replace(' ', '_', $name),
'type' => $type,
'name' => $name,
];
$options->add_field($optionFields);
}
}
}
On Frontend, I need to check the field type
Something like that
$fields = get_post_meta($post_id);
foreach ($fields as $field) {
if($field['type'] == 'select') {
// Do something
}
if($field['type'] == 'text') {
// Do something else
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
