i'm a absolute noob with php coding and I guess after several laughs, the solution is really simple for a professional. I coded a simple average-calculation for the category.php of my wp-template and it works perfectly but I heard that it's the much better way to split design and function and so I tried to convet this to a template function:
<?php
$total = 0;
$count = 0;
foreach($posts as $post)
{
if(get_field('weight')){ // If we have a value add it to the total and count it
$total += get_field('weight');
$count++;
}
}
$Average = $total / $count;
echo $Average;
?>
The result is always "0" because I guess he pulls the variable from above and skips the foreach-procedure. But why?
function averageit()
{
$total = 0;
$count = 0;
foreach($posts as $post)
if(get_field('weight'))
$total += get_field('weight');
$count++;
$Average = $total / $count;
echo $Average;
}
I hope somebody is able to help me. Thank you in advance!