Splitting 'unlimited' body fields I have set the 'Body field settings' > 'Number of values' to 'Unlimited'; so the user can add as many 'body' fields as they like.

I have then added the following piece of code to my themes 'node.tpl.php' file:

&lt;?php $i=0; 
while ($i&lt;=10) : ?&gt; 
&lt;section class="box page-box"&gt; 
&lt;?php print render($content['body'][$i]); 
$i++; ?&gt;
&lt;/section&gt;
&lt;?php endwhile; ?&gt; 


This code outputs each of the 'body' fields in a section/div and then adds a number of blank sections/divs until it hits 10! 

How would I find out the total number of 'body' fields so that it only outputs the available number and not all 10? I was trying to use 'foreach' but couldn't seem to get it working.
This issue was solved using a function in my template.php file to get the field data out of entities, which I found here. 

/**
 * Returns field values as actual entities where possible,
 * also allows selection of individual items to be returned
 */
function field_fetch_field_values($entity_type, $entity, $field_name, $get_delta = NULL, $get_key = NULL) {
  $values = array();
  if (isset($entity-&gt;$field_name) &amp;&amp; !empty($entity-&gt;$field_name)) {
    foreach (field_get_items($entity_type, $entity, $field_name) as $delta =&gt; $item) {
      $value = $item;
      $keys = array_keys($item);
      if (count($keys)==1) {
        $key = $keys[0];
        switch ($key) {
          case 'nid':
            $value = array_shift(entity_load('node', array($item[$key])));
            break;
          case 'uid':
            $value = array_shift(entity_load('user', array($item[$key])));
            break;
          case 'tid':
            $value = array_shift(entity_load('taxonomy_term', array($item[$key])));
            break;
          case 'vid':
            $value = array_shift(entity_load('taxonomy_vocabulary', array($item[$key])));
            break;
          case 'value':
            $value = $item['value'];
            break;
        }
      }
      else {
        if ($get_key &amp;&amp; isset($item[$get_key])) {
          $value = $item[$get_key];
        }
        elseif (array_key_exists('value', $item)) {
          $value = isset($item['safe_value']) ? $item['safe_value'] : $item['value'];
        }
      }
      $values[$delta] = $value;
    }
  }
  if (is_numeric($get_delta)) {
    return isset($values[$get_delta]) ? $values[$get_delta] : NULL;
  }
  return $values;
}


I then created the code below in my node.tpl page to check how many 'body' fields were in the array.

&lt;?php if (render($content)) :
$contArray = array();
$summary = field_fetch_field_values('node', $node, 'body');
$contArray = $summary;
foreach ($contArray as $value) : ?&gt;
&lt;section class="box page-box"&gt; 
&lt;?php print $value; ?&gt;
&lt;/section&gt;
&lt;?php endforeach; else : ?&gt;
&lt;section class="box page-box"&gt; 
&lt;h2&gt;There is currently no content for this page.&lt;/h2&gt;
&lt;/section&gt;  
&lt;?php endif; ?&gt;