06/24/2015 at 10:24 am
#338416
Keymaster
@ James
re: How can I display a group of checkboxes together? For example, we have 9 different certifications that a person may have.
You would need to use a checklist field These are only marginally more difficult to work with compared to the text field and checkbox field types. Building off the previous samples here’s how to add a checklist:
$atts = array(
'title' => 'Metabox Name', // Change this to a name which applies to your project.
'id' => 'custom_one', // Change this so it is unique to you project.
'context' => 'normal',
'priority' => 'core',
'fields' => array(
array(
'name' => 'Field Name One', // Change this field name to something which applies to you project.
'show_label' => TRUE, // Whether or not to display the 'name'. Changing it to false will suppress the name.
'id' => 'field_id_1', // Change this so it is unique to you project. Each field id MUST be unique.
'type' => 'text', // This is the field type being added.
'size' => 'regular', // This can be changed to one of the following: 'small', 'regular', 'large'
),
array(
'name' => 'Field Name Two', // Change this field name to something which applies to you project.
'show_label' => TRUE, // Whether or not to display the 'name'. Changing it to false will suppress the name.
'id' => 'field_id_2', // Change this so it is unique to you project. Each field id MUST be unique.
'type' => 'text', // This is the field type being added.
'size' => 'regular', // This can be changed to one of the following: 'small', 'regular', 'large'
),
array(
'name' => 'Field Name Three', // Change this field name to something which applies to you project.
'show_label' => TRUE, // Whether or not to display the 'name'. Changing it to false will suppress the name.
'desc' => 'Checkbox Description',
'id' => 'field_id_3', // Change this so it is unique to you project. Each field id MUST be unique.
'type' => 'checkbox', // This is the field type being added.
),
array(
'name' => 'Field Name Four', // Change this field name to something which applies to you project.
'show_label' => TRUE, // Whether or not to display the 'name'. Changing it to false will suppress the name.
'desc' => 'Checkbox Description',
'id' => 'field_id_4', // Change this so it is unique to you project. Each field id MUST be unique.
'type' => 'checkbox', // This is the field type being added.
),
array(
'name' => 'Field Name Five', // Change this field name to something which applies to you project.
'show_label' => TRUE, // Whether or not to display the 'name'. Changing it to false will suppress the name.
'desc' => 'Checkbox Description',
'id' => 'field_id_5', // Change this so it is unique to you project. Each field id MUST be unique.
'type' => 'checkbox', // This is the field type being added.
),
array(
'name' => 'Field Name Six', // Change this field name to something which applies to you project.
'show_label' => TRUE, // Whether or not to display the 'name'. Changing it to false will suppress the name.
'id' => 'certifications',
'type' => 'checkboxgroup',
'options' => array(
'RPR (Registered Prefessional Reporter)' => 'RPR (Registered Prefessional Reporter)',
'RMR (Registered Merit Reporter)' => 'RMR (Registered Merit Reporter)',
'RDR (Registered Diplomat Reporter)' => 'RDR (Registered Diplomat Reporter)',
'CRR (Certified Realtime Reporter)' => 'CRR (Certified Realtime Reporter)',
'CSR (Certified Shorthand Reporter)' => 'CSR (Certified Shorthand Reporter)',
'CLVS (Certified Legal Video Specialist)' => 'CLVS (Certified Legal Video Specialist)',
'CMRS (Certified Manager of Reporting Services)' => 'CMRS (Certified Manager of Reporting Services)',
'CRI (Court Reporting Instructor)' => 'CRI (Court Reporting Instructor)',
'CPE (Certified Program Evaluator)' => 'CPE (Certified Program Evaluator)',
),
'default' => '',
),
),
);
Displaying this is where is gets “tricky” because it is stored as an array so you have to loop thru the saved values to display them like so:
$certifications = $entry->getMeta(
array(
'key' => 'certifications', // This should match exactly the field id used when registering the custom field.
'single' => TRUE // Do not change this.
)
);
if ( ! empty( $certifications ) ) {
echo '<ul class="cn-certifications">';
foreach ( $certifications as $certification ) {
printf( '<li class="cn-certification cn-%1$s">%2$s</li>', esc_attr( $certification ), esc_html( $certification ) );
}
echo '</ul>';
}
This is not tested, but should work. Hope that helps!
