Setup page template array in a custom post type

Hi below is a ‘case’ (in a custom post type) for pulling all categories and displaying them as a drop-down menu. How can I do the same but turn it into a list of checkboxes?

    //tax_select - this lists all of the categories in a drop-down, we want to change to a checkbox
case 'tax_select':
echo '<select name="'.$field['id'].'" id="'.$field['id'].'">
        //<option value="">Select One</option>'; // Select One
$terms = get_terms($field['id'], 'get=all');
$selected = wp_get_object_terms($post->ID, $field['id']);
foreach ($terms as $term) {
    if (!empty($selected) && !strcmp($term->slug, $selected[0]->slug))
        echo '<option value="'.$term->slug.'" selected="selected">'.$term->name.'</option>';
    else
        echo '<option value="'.$term->slug.'">'.$term->name.'</option>';
}
$taxonomy = get_taxonomy($field['id']);
echo '</select><br /><span class="description"><a href="'.get_bloginfo('home').'/wp-admin/edit-tags.php?taxonomy='.$field['id'].'">Manage '.$taxonomy->label.'</a></span>';
break;

How I’m saving the data:

    // 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) {
            if($field['type'] == 'tax_select') continue;
    $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

    // save taxonomies
$post = get_post($post_id);
$category = $_POST['category'];
wp_set_object_terms( $post_id, $category, 'category' );
}
add_action('save_post', 'save_custom_meta');  

Answer

//tax_select - this lists all of the categories in a drop-down, we want to change to a checkbox
case 'tax_select':
    $terms = get_terms( $field['id'], 'get=all' );
    $selected = wp_get_object_terms( $post->ID, $field['id'] );
    foreach ( $terms as $term ) {
        printf( '<input type="checkbox" name="%s[]" value="%s" %s/><br />', $field['id'], $term->slug, !empty( $selected ) && !strcmp( $term->slug, $selected[0]->slug ) ? ' checked="checked"' : '' );
    }
    $taxonomy = get_taxonomy( $field['id'] );
    echo '<span class="description"><a href="' . get_bloginfo( 'home' ) . '/wp-admin/edit-tags.php?taxonomy=' . $field['id'] . '">Manage ' . $taxonomy->label . '</a></span>';
break;

Attribution
Source : Link , Question Author : Rob Myrick , Answer Author : Eugene Manuilov

Leave a Comment