Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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');  
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted
//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;
share|improve this answer
    
Thank you! The checkboxes are not saving the checked values. I posted my save function so that you could see what's going on. Any suggestions? –  Rob Myrick Jun 13 '12 at 5:26
    
OK....after checking again...here's what is happening. When I check a single checkbox the first time, it is stored. But when I try to select any additonal checkboxes after that it doesn't save the latter choices. –  Rob Myrick Jun 13 '12 at 5:36
    
Hi Eugene, would you be willing to review your code to see why only one checkbox can be selected. I'm so close! I really appreciate your help if you have the time –  Rob Myrick Jun 14 '12 at 3:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.