• just created a kind of long metabox using a tutorial at wptuts but I am lost in two things:

    The output of the metabox displays all data grouped (ex. in grouped checkboxes they are all in a whole even if I have different groups) and I’d like to grab the titles of each group and display them in different tables, divs or blocks.

    Don’t know how to display the saved data in the post, and also display it in separate blocks.

    My code is this:

    $prefix = 'dbt_';
    $meta_box = array(
    'id' => 'features',
    'title' => 'Property Features',
    'page' => 'post',
    'context' => 'normal',
    'priority' => 'high',
    'fields' => array(
        array(
            'name' => 'Price (in USD)',
            'desc' => '',
            'id' => $prefix . 'text',
            'type' => 'text',
            'std' => ' '
        ),
    
          array(
            'name' => 'Price (in Mexican Peso)',
            'desc' => '',
            'id' => $prefix . 'text',
            'type' => 'text',
            'std' => ' '
        ),
    
        array(
            'name' => 'Address',
            'desc' => '',
            'id' => $prefix . 'textarea',
            'type' => 'textarea',
            'std' => ''
        ),
    
        array(
            'name' => 'Property for',
            'id' => $prefix . 'select',
            'type' => 'select',
            'options' => array('Sale', 'Rent', 'Vacational Rental')
        ),
    
        array(
            'name' => 'Area (m2)',
            'desc' => '',
            'id' => $prefix . 'text',
            'type' => 'text',
            'std' => ' '
        ),
    
        array(
            'name' => 'Area of terrain (m2)',
            'desc' => '',
            'id' => $prefix . 'text',
            'type' => 'text',
            'std' => ' '
        ),
    
        array(
            'name' => 'Number of rooms',
            'desc' => '',
            'id' => $prefix . 'text',
            'type' => 'text',
            'std' => ' '
        ),
    
        array(
            'name' => 'Number of bathrooms. Include half bathrooms (ex. 2 1/2)',
            'desc' => '',
            'id' => $prefix . 'text',
            'type' => 'text',
            'std' => ' '
        ),
    
        array(
            'name' => 'Service Bathroom',
            'id' => $prefix . 'checkbox',
            'type' => 'checkbox'
    ),
    
    array(
            'name' => 'Furnished',
            'id' => $prefix . 'checkbox',
            'type' => 'checkbox'
    ),
    array(
            'label' => 'Property specs',
            'desc'  => '',
            'id'    => $prefix.'checkbox_group',
            'type'  => 'checkbox_group',
            'options' => array (
            'one' => array (
            'label' => 'Livingroom',
            'value' => 'one'
        ),
            'two' => array (
            'label' => 'Diningroom',
            'value' => 'two'
        ),
            'three' => array (
            'label' => 'Breackfast area',
            'value' => 'three'
        ), 
    
            'four' => array (
            'label' => 'TV room',
            'value' => 'four'
        ),
    
            'five' => array (
            'label' => 'Studio',
            'value' => 'five'
        ),
    
            'six' => array (
            'label' => 'Integrated Kitchen',
            'value' => 'six'
        ),
    
            'seven' => array (
            'label' => 'Laundry Room',
            'value' => 'seven'
        ),
    
            'eight' => array (
            'label' => 'Service room',
            'value' => 'eight'
        ),
    
            'nine' => array (
            'label' => 'jacuzzi',
            'value' => 'nine'
        ),
    
            'ten' => array (
            'label' => 'terrace',
            'value' => 'ten'
        ),
    
            'eleven' => array (
            'label' => 'balcony',
            'value' => 'eleven'
        ),
    
            'twelve' => array (
            'label' => 'Warehouse',
            'value' => 'twelve'
        )
    )
    ),  
    
        array(
            'label' => 'Installed',
            'desc'  => '',
            'id'    => $prefix.'checkbox_group',
            'type'  => 'checkbox_group',
            'options' => array (
            'one' => array (
            'label' => 'Air conditioner',
            'value' => 'one'
        ),
            'two' => array (
            'label' => 'Stationary gas',
            'value' => 'two'
        ),
            'three' => array (
            'label' => 'Water heater',
            'value' => 'three'
        ), 
    
            'four' => array (
            'label' => 'Hurricain shutters or hurricain protection',
            'value' => 'four'
        ),
    
            'five' => array (
            'label' => 'Telephone line',
            'value' => 'five'
        )
    )
    ),
    
        array(
            'label' => 'Outdoors',
            'desc'  => '',
            'id'    => $prefix.'checkbox_group',
            'type'  => 'checkbox_group',
            'options' => array (
            'one' => array (
            'label' => 'Parking space',
            'value' => 'one'
        ),
            'two' => array (
            'label' => 'Garden',
            'value' => 'two'
        ),
            'three' => array (
            'label' => 'Pool',
            'value' => 'three'
        ), 
    
            'four' => array (
            'label' => 'Tennis court',
            'value' => 'four'
        ),
    
            'five' => array (
            'label' => 'Children playground',
            'value' => 'five'
        )
    )
    ),
    
        array(
            'label' => 'Facilities',
            'desc'  => '',
            'id'    => $prefix.'checkbox_group',
            'type'  => 'checkbox_group',
            'options' => array (
            'one' => array (
            'label' => 'Spa',
            'value' => 'one'
        ),
            'two' => array (
            'label' => 'Gym',
            'value' => 'two'
        ),
            'three' => array (
            'label' => 'Party salon',
            'value' => 'three'
        ), 
    
            'four' => array (
            'label' => 'Snack-bar',
            'value' => 'four'
        )
    
    )
    ),
    
        array(
            'label' => 'Luxury Features',
            'desc'  => '',
            'id'    => $prefix.'checkbox_group',
            'type'  => 'checkbox_group',
            'options' => array (
            'one' => array (
            'label' => 'Clubhouse',
            'value' => 'one'
        ),
            'two' => array (
            'label' => 'Dock',
            'value' => 'two'
        ),
            'three' => array (
            'label' => 'Waterfront',
            'value' => 'three'
        ), 
    
            'four' => array (
            'label' => 'Golf course',
            'value' => 'four'
        )
    
    )
    )
    
    )
    );
    
    add_action('admin_menu', 'mytheme_add_box');
    // Add meta box
    function mytheme_add_box() {
    global $meta_box;
    add_meta_box($meta_box['id'], $meta_box['title'], 'mytheme_show_box', $meta_box['page'],    $meta_box['context'], $meta_box['priority']);
    }
    
    // Callback function to show fields in meta box
    function mytheme_show_box() {
    global $meta_box, $post;
    // Use nonce for verification
    echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
    echo '<div class="form-table">';
    foreach ($meta_box['fields'] as $field) {
        // get current post meta data
        $meta = get_post_meta($post->ID, $field['id'], true);
        echo '<tr>',
                '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
                '<td>';
        switch ($field['type']) {
            case 'text':
                echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc'];
                break;
            case 'textarea':
                echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '<br />', $field['desc'];
                break;
            case 'select':
                echo '<select name="', $field['id'], '" id="', $field['id'], '">';
                foreach ($field['options'] as $option) {
                    echo '<option ', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
                }
                echo '</select>';
                break;
            case 'radio':
                foreach ($field['options'] as $option) {
                    echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
                }
                break;
            case 'checkbox':
                echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
                break;
    
    // checkbox_group
    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;    
    
        }
        echo     '</td><td>',
            '</td></tr>';
    }
    echo '</table>';
    }
    
    do_action('save_post', 'mytheme_save_data');
    // Save data from meta box
    function mytheme_save_data($post_id) {
    global $meta_box;
    // verify nonce
    if (!wp_verify_nonce($_POST['mytheme_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;
    }
    foreach ($meta_box['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);
        }
    }
    }
    
    // check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    return $post_id;
    }

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The titles appear to each be in their own <th> block for each table row, so getting the right appearance should be a matter of applying the correct CSS directives. Using a CSS analyzer such as Firebug for Firefox or as bundled with other browsers makes it much easier to get the correct CSS relatively quickly.

    To display the values in posts, all the values are stored in post meta, so use get_post_meta() on whatever template the post is using to get the actual value, and wrap it in whatever HTML is needed to present it properly. You probably do not want to have the default template outputting all this stuff, so to get a custom template used just for certain posts, either create a custom post type or a custom page template.

    You also need to adjust your fields array IDs so they are all unique. Not only is this needed to have compliant HTML, but if you do not, each field will overwrite the previous field value in post meta.

    Thread Starter ikosweb

    (@ikosweb)

    Hello, can you please tell me how to use different tables in each group of fields?

    Thanks

    Moderator bcworkz

    (@bcworkz)

    I’m not sure what you want, other than the checkbox groups, the fields do not belong to groups. Do you want just separate tables for the checkbox groups or separate tables for every field?

    The difference is where you echo out the <table> and </table> tags. If just the checkbox groups, echo the tags at the beginning and end of the case ‘checkbox_group’ segment, right after the case line and right before the corresponding break; line. This will place sub-tables within the main table.

    If you wanted a table for each field, single or not, locate where the initial <table> tag is output. It is not in the snippet provided. Move it from there to below the first foreach line of the mytheme_show_box() function. Then swap places of the closing echo '</table>'; line with the curly bracket ‘}’ above it so that it is inside the foreach block instead of outside.

    I hope that makes sense. If not, please better describe what you want in each table and then I can post example code for you to use.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to separate data in wordpress metabox?’ is closed to new replies.