06/23/2015 at 4:20 pm
#338260
Keymaster
@ James
Adding a checkbox is only mildly more difficult. Here’s the code that to add two text fields and three checkboxes:
$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.
),
),
);
Displaying this in the template is a bit different because the checkbox is saved as either 1
(checked) or 0
(not checked). So you would want to do this to display it in the template:
$checked = $entry->getMeta(
array(
'key' => 'field_id', // This should match exactly the field id used when registering the custom field.
'single' => TRUE // Do not change this.
)
);
echo if ( $checked ) '<p>Checkbox was checked.</p>';
Hope that helps!