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.

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?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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
        }
    }
}
share|improve this answer
    
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
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.