0

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?

1

You can add another simple foreach loop to check if you have any values first:

foreach ( $meta_boxes as $metabox ) {
    $has_value = false
    foreach ( $metabox['fields'] as $field ) {
        $meta = get_post_meta($post->ID, $field['id'], true); 
        if(!isset($meta[0])){
            $need_title = true;
            break;
        }
    }
    if ($has_value){
        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
        }
    }
}
  • Great idea Bainternet, thanks! Instead of having the $need_title var I just echo the metabox title where if(isset($meta[0])) – at least three characters Dec 13 '11 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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