Custom Meta Box Checkbox not printing
-
I’m creating my own theme and wan’t to make a checkbox on every post that says something like if this is “checked” print “this image” my problems is that i cant seem to get anything from my custom meta box data on my frontend heres my custom meta box code:
// Add the Meta Box function add_custom_meta_box() { add_meta_box( 'custom_meta_box', // $id 'Programs used', // $title 'show_custom_meta_box', // $callback 'portfolio', // $page 'normal', // $context 'high'); // $priority } add_action('add_meta_boxes', 'add_custom_meta_box'); // Field Array $prefix = 'custom_'; $custom_meta_fields = array( array ( 'label' => '2D Programs', 'desc' => 'A description for the field.', 'id' => $prefix.'checkbox2D_group', 'type' => 'checkbox_group', 'options' => array ( 'one' => array ( 'label' => 'Photoshop', 'value' => 'one' ), 'two' => array ( 'label' => 'Illustrator', 'value' => 'two' ) ) ), array ( 'label' => '3D Programs', 'desc' => 'A description for the field.', 'id' => $prefix.'checkbox3D_group', 'type' => 'checkbox_group', 'options' => array ( 'three' => array ( 'label' => 'Maya', 'value' => 'three' ), 'four' => array ( 'label' => 'zBrush', 'value' => 'four' ) ) ), ); // The Callback function show_custom_meta_box() { global $custom_meta_fields, $post; // Use nonce for verification echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />'; // Begin the field table and loop echo '<table class="form-table">'; foreach ($custom_meta_fields as $field) { // get value of this field if it exists for this post $meta = get_post_meta($post->ID, $field['id'], true); // begin a table row with echo '<tr> <th><label for="'.$field['id'].'">'.$field['label'].'</label></th> <td>'; switch($field['type']) { // checkbox case 'checkbox_group': foreach ($field['options'] as $option) { echo '<input type="checkbox" value="'.$option['value'].'" name="'.$field['id'].'[]" id="'.$option['value'].'"',$meta && in_array($option['value'], $meta) ? ' checked="checked"' : '',' /> <label for="'.$option['value'].'">'.$option['label'].'</label><br />'; } echo '<span class="description">'.$field['desc'].'</span>'; break; } //end switch echo '</td></tr>'; } // end foreach echo '</table>'; // end table } // Save the Data function save_custom_meta($post_id) { global $custom_meta_fields; // verify nonce if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__))) return $post_id; // check autosave if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id; // check permissions if ('page' == $_POST['post_type']) { if (!current_user_can('edit_page', $post_id)) return $post_id; } elseif (!current_user_can('edit_post', $post_id)) { return $post_id; } // loop through fields and save the data foreach ($custom_meta_fields as $field) { $old = get_post_meta($post_id, $field['id'], true); $new = $_POST[$field['id']]; if ($new && $new != $old) { update_post_meta($post_id, $field['id'], $new); } elseif ('' == $new && $old) { delete_post_meta($post_id, $field['id'], $old); } } // end foreach } add_action('save_post', 'save_custom_meta', 10, 2); /*
I followed a tutorial and it did not tell how to get it out on the front-end. and it could be something with my saev function too thats wrong maybe.
- The topic ‘Custom Meta Box Checkbox not printing’ is closed to new replies.