Hello guys,
I’m really not a professional coder so excuse any nooby mistakes or words. Will try my best to explain my actual trouble. First here is the code that drives me insane:
function get_my_values($posts) {
$total = 0;
$count = 0;
foreach($posts as $post)
{
if(get_field('faktor'))
{
$total += get_field('faktor');
$count++;
}
}
$Average = $total / $count;
echo $Average;
}
This function works perfect in the category view(category.php) but not in the single-view (single.php). In the category.php the system is using all values of the custom-field “faktor” and creates a sum like it should be but in the single.php the system only works with the custom-field value of the single-post.
Actually I think the whole function is straight for the trash but I’m not afraid of learning something new or spending my time with google like the last 5 days :-D. But I’m thankful for any idea to make a step in front.
Thank you in advance!
Hi @noterepeat
I think all you need to do is make sure that ACF is loading from the correct $post by adding in a second parameter into the get_field function like so:
get_field('faktor', $post->ID)
Hope that helps.
Thanks
E
Hello E,
thank you for your quick support! Doesn’t worked for me but after another night with google I found the solution!
$total = 0;
query_posts('category_name=' . get_option('featured-cat'));
while (have_posts()) : the_post();
$total += get_post_meta($post->ID, 'weight', true);
endwhile;
echo '<p>'.$total.'</p>';
Did the post-count for creating the final average with a seperate function I found on the net!