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 am trying to calculate the sum total from two custom sub-fields using ACF. The custom field is called 'repeater' and the sub-field is called 'space_avail'. I've tried the code below but it just lists the numbers individually rather than adding them up to a total. Please help.

<?php 
  $total = 0;
  while(the_repeater_field('repeater' )): 
      the_sub_field('space_avail' );
      $total += get_sub_field('space_avail' );
  endwhile; 
  echo $total;
?>
share|improve this question
add comment

1 Answer

<?php 
  $total = 0;
  while(the_repeater_field('repeater' )): 
      the_sub_field('space_avail' );
      $total += intval( get_sub_field('space_avail' ) );
  endwhile; 
  echo $total;
?>

Notice the intval. It tells php to consider the result as an number, not as a string

share|improve this answer
    
Thanks for the reply. Unfortunately, the numbers are still being returned as a string. –  John Dec 18 '12 at 18:26
    
It must be something else. Have you consulted the plugin's documentation to check if those functions are correct? –  Mridul Aggarwal Dec 18 '12 at 18:37
    
If no one here is able to help, post it on plugin's forum. You'll surely get an answer there. –  Mridul Aggarwal Dec 18 '12 at 18:45
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.