Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to separate data in wordpress metabox?
    primarykey
    data
    text
    <p>I just created a kind of long metabox using a tutorial at <a href="http://wp.tutsplus.com/tutorials/reusable-custom-meta-boxes-part-1-intro-and-basic-fields/" rel="nofollow">wptuts</a> but I am lost in two things:</p> <ol> <li><p>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.</p></li> <li><p>Don't know how to display the saved data in the post, and also display it in separate blocks.</p></li> </ol> <p>My code is this:</p> <pre><code>&lt;?php $prefix = 'dbt_'; $meta_box = array( 'id' =&gt; 'features', 'title' =&gt; 'Property Features', 'page' =&gt; 'post', 'context' =&gt; 'normal', 'priority' =&gt; 'high', 'fields' =&gt; array( array( 'name' =&gt; 'Price (in USD)', 'desc' =&gt; '', 'id' =&gt; $prefix . 'text', 'type' =&gt; 'text', 'std' =&gt; ' ' ), array( 'name' =&gt; 'Price (in Mexican Peso)', 'desc' =&gt; '', 'id' =&gt; $prefix . 'text', 'type' =&gt; 'text', 'std' =&gt; ' ' ), array( 'name' =&gt; 'Address', 'desc' =&gt; '', 'id' =&gt; $prefix . 'textarea', 'type' =&gt; 'textarea', 'std' =&gt; '' ), array( 'name' =&gt; 'Property for', 'id' =&gt; $prefix . 'select', 'type' =&gt; 'select', 'options' =&gt; array('Sale', 'Rent', 'Vacational Rental') ), array( 'name' =&gt; 'Area (m2)', 'desc' =&gt; '', 'id' =&gt; $prefix . 'text', 'type' =&gt; 'text', 'std' =&gt; ' ' ), array( 'name' =&gt; 'Area of terrain (m2)', 'desc' =&gt; '', 'id' =&gt; $prefix . 'text', 'type' =&gt; 'text', 'std' =&gt; ' ' ), array( 'name' =&gt; 'Number of rooms', 'desc' =&gt; '', 'id' =&gt; $prefix . 'text', 'type' =&gt; 'text', 'std' =&gt; ' ' ), array( 'name' =&gt; 'Number of bathrooms. Include half bathrooms (ex. 2 1/2)', 'desc' =&gt; '', 'id' =&gt; $prefix . 'text', 'type' =&gt; 'text', 'std' =&gt; ' ' ), array( 'name' =&gt; 'Service Bathroom', 'id' =&gt; $prefix . 'checkbox', 'type' =&gt; 'checkbox' ), array( 'name' =&gt; 'Furnished', 'id' =&gt; $prefix . 'checkbox', 'type' =&gt; 'checkbox' ), array( 'label' =&gt; 'Property specs', 'desc' =&gt; '', 'id' =&gt; $prefix.'checkbox_group', 'type' =&gt; 'checkbox_group', 'options' =&gt; array ( 'one' =&gt; array ( 'label' =&gt; 'Livingroom', 'value' =&gt; 'one' ), 'two' =&gt; array ( 'label' =&gt; 'Diningroom', 'value' =&gt; 'two' ), 'three' =&gt; array ( 'label' =&gt; 'Breackfast area', 'value' =&gt; 'three' ), 'four' =&gt; array ( 'label' =&gt; 'TV room', 'value' =&gt; 'four' ), 'five' =&gt; array ( 'label' =&gt; 'Studio', 'value' =&gt; 'five' ), 'six' =&gt; array ( 'label' =&gt; 'Integrated Kitchen', 'value' =&gt; 'six' ), 'seven' =&gt; array ( 'label' =&gt; 'Laundry Room', 'value' =&gt; 'seven' ), 'eight' =&gt; array ( 'label' =&gt; 'Service room', 'value' =&gt; 'eight' ), 'nine' =&gt; array ( 'label' =&gt; 'jacuzzi', 'value' =&gt; 'nine' ), 'ten' =&gt; array ( 'label' =&gt; 'terrace', 'value' =&gt; 'ten' ), 'eleven' =&gt; array ( 'label' =&gt; 'balcony', 'value' =&gt; 'eleven' ), 'twelve' =&gt; array ( 'label' =&gt; 'Warehouse', 'value' =&gt; 'twelve' ) ) ), array( 'label' =&gt; 'Installed', 'desc' =&gt; '', 'id' =&gt; $prefix.'checkbox_group', 'type' =&gt; 'checkbox_group', 'options' =&gt; array ( 'one' =&gt; array ( 'label' =&gt; 'Air conditioner', 'value' =&gt; 'one' ), 'two' =&gt; array ( 'label' =&gt; 'Stationary gas', 'value' =&gt; 'two' ), 'three' =&gt; array ( 'label' =&gt; 'Water heater', 'value' =&gt; 'three' ), 'four' =&gt; array ( 'label' =&gt; 'Hurricain shutters or hurricain protection', 'value' =&gt; 'four' ), 'five' =&gt; array ( 'label' =&gt; 'Telephone line', 'value' =&gt; 'five' ) ) ), array( 'label' =&gt; 'Outdoors', 'desc' =&gt; '', 'id' =&gt; $prefix.'checkbox_group', 'type' =&gt; 'checkbox_group', 'options' =&gt; array ( 'one' =&gt; array ( 'label' =&gt; 'Parking space', 'value' =&gt; 'one' ), 'two' =&gt; array ( 'label' =&gt; 'Garden', 'value' =&gt; 'two' ), 'three' =&gt; array ( 'label' =&gt; 'Pool', 'value' =&gt; 'three' ), 'four' =&gt; array ( 'label' =&gt; 'Tennis court', 'value' =&gt; 'four' ), 'five' =&gt; array ( 'label' =&gt; 'Children playground', 'value' =&gt; 'five' ) ) ), array( 'label' =&gt; 'Facilities', 'desc' =&gt; '', 'id' =&gt; $prefix.'checkbox_group', 'type' =&gt; 'checkbox_group', 'options' =&gt; array ( 'one' =&gt; array ( 'label' =&gt; 'Spa', 'value' =&gt; 'one' ), 'two' =&gt; array ( 'label' =&gt; 'Gym', 'value' =&gt; 'two' ), 'three' =&gt; array ( 'label' =&gt; 'Party salon', 'value' =&gt; 'three' ), 'four' =&gt; array ( 'label' =&gt; 'Snack-bar', 'value' =&gt; 'four' ) ) ), array( 'label' =&gt; 'Luxury Features', 'desc' =&gt; '', 'id' =&gt; $prefix.'checkbox_group', 'type' =&gt; 'checkbox_group', 'options' =&gt; array ( 'one' =&gt; array ( 'label' =&gt; 'Clubhouse', 'value' =&gt; 'one' ), 'two' =&gt; array ( 'label' =&gt; 'Dock', 'value' =&gt; 'two' ), 'three' =&gt; array ( 'label' =&gt; 'Waterfront', 'value' =&gt; 'three' ), 'four' =&gt; array ( 'label' =&gt; 'Golf course', 'value' =&gt; '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 '&lt;input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" /&gt;'; echo '&lt;div class="form-table"&gt;'; foreach ($meta_box['fields'] as $field) { // get current post meta data $meta = get_post_meta($post-&gt;ID, $field['id'], true); echo '&lt;tr&gt;', '&lt;th style="width:20%"&gt;&lt;label for="', $field['id'], '"&gt;', $field['name'], '&lt;/label&gt;&lt;/th&gt;', '&lt;td&gt;'; switch ($field['type']) { case 'text': echo '&lt;input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" /&gt;', '&lt;br /&gt;', $field['desc']; break; case 'textarea': echo '&lt;textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%"&gt;', $meta ? $meta : $field['std'], '&lt;/textarea&gt;', '&lt;br /&gt;', $field['desc']; break; case 'select': echo '&lt;select name="', $field['id'], '" id="', $field['id'], '"&gt;'; foreach ($field['options'] as $option) { echo '&lt;option ', $meta == $option ? ' selected="selected"' : '', '&gt;', $option, '&lt;/option&gt;'; } echo '&lt;/select&gt;'; break; case 'radio': foreach ($field['options'] as $option) { echo '&lt;input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' /&gt;', $option['name']; } break; case 'checkbox': echo '&lt;input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' /&gt;'; break; // checkbox_group case 'checkbox_group': foreach ($field['options'] as $option) { echo '&lt;input type="checkbox" value="'.$option['value'].'" name="'.$field['id'].'[]" id="'.$option['value'].'"',$meta &amp;&amp; in_array($option['value'], $meta) ? ' checked="checked"' : '',' /&gt; &lt;label for="'.$option['value'].'"&gt;'.$option['label'].'&lt;/label&gt;&lt;br /&gt;'; } echo '&lt;span class="description"&gt;'.$field['desc'].'&lt;/span&gt;'; break; } echo '&lt;/td&gt;&lt;td&gt;', '&lt;/td&gt;&lt;/tr&gt;'; } echo '&lt;/table&gt;'; } 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') &amp;&amp; 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 &amp;&amp; $new != $old) { update_post_meta($post_id, $field['id'], $new); } elseif ('' == $new &amp;&amp; $old) { delete_post_meta($post_id, $field['id'], $old); } } } // check autosave if (defined('DOING_AUTOSAVE') &amp;&amp; DOING_AUTOSAVE) { return $post_id; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload