I'm using multiple metabox panels in my write posts. IN my functions I'm registering multiple metaboxes:
$prefix = 'dbt_';
$meta_boxes = array();
$meta_boxes[] = array(
'id' => 'general_information',
'title' => 'General Information',
'pages' => array('post', 'page', 'link'), // multiple post types, accept custom post types
'context' => 'normal', // normal, advanced, side (optional)
'priority' => 'high', // high, low (optional)
'fields' => array(
array(
'name' => 'Name',
'id' => $prefix . 'name',
'type' => 'text',
'std' => 'default value here'
),
array(
'name' => 'Manufacturer',
'id' => $prefix . 'manufacturer',
'type' => 'text',
'std' => ''
)
$meta_boxes[] = array(
'id' => 'measurements',
'title' => 'Measurements',
'pages' => array('post', 'page', 'link'), // multiple post types, accept custom post types
'context' => 'normal', // normal, advanced, side (optional)
'priority' => 'high', // high, low (optional)
'fields' => array(
array(
'name' => 'Length',
'id' => $prefix . 'length',
'type' => 'text',
'std' => ''
),
array(
'name' => 'Width',
'id' => $prefix . 'width',
'type' => 'text',
'std' => ''
)
..etc
In my theme I'm displaying the name and values of each field in each metabox group. The goal is to have fields grouped by metaboxes otherwise I would just use get_post_custom($post_id)
.
Code in single.php:
global $meta_boxes;
foreach ( $meta_boxes as $metabox ) {
echo $metabox['title'];
foreach ( $metabox['fields'] as $field ) {
$meta = get_post_meta($post->ID, $field['id'], true); //get post meta from each metabox
if(!isset($meta[0])) continue; //display only fields with values
echo $field['name'];
echo $meta ? $meta : $field['std']; //show value or default value
}
}
If for instance a metabox has no fields with values, the metabox title $metabox['title']
still appears. How can I hide the metabox title if no values exists?