Getting custom fields from all posts within a post
-
Hello,
I currently have a function that calls and adds a custom field from each post. (example: Post #1: apples=1 Post #2 apples=1 total apples=2)
Whenever I got into a specific post, it negates getting the custom fields from all posts and gets in from the post I’m currently in instead. (example: apples=1)
Is there anyway around this? Does this code look correct for what I want to accomplish?
What I want (Home Page)
What I’m getting on the postsLooping code:
<div id="stats-current-weight"> <?php $totalLostWeight = 0; $startWeight = 297.0; while (have_posts()) : the_post(); $postmeta = get_post_custom($post->ID); $totalLostWeight += (int) $postmeta['weight-lost'][0]; endwhile; $lost = $startWeight + $totalLostWeight; echo "<p class='stats-number'>" . $lost . "</p>"; ?> <p class="stats-text">current weight</p> </div> <div id="stats-weight-lost"> <?php $totalLost = 0; while (have_posts()) : the_post(); $postmeta = get_post_custom($post->ID); $totalLost -= (float) $postmeta['weight-lost'][0]; endwhile; echo "<p class='stats-number'>" . $totalLost . "</p>"; ?> <p class="stats-text">weight lost</p> </div> <div id="stats-exercise-time"> <?php function formatTime($total){ $days = $hours = $minutes = 0; // 1440 = number minutes in a day. if($total > 1440){ $days = intval($total / 1440); $total = $total % 1440; } if($total > 60){ $hours = intval($total / 60); $minutes = $total % 60; } if ($days < 10) { $days = "0" . $days; } if ($hours < 10) { $hours = "0" . $hours; } if ($minutes < 10) { $minutes = "0" . $minutes; } return "$days:$hours:$minutes"; } $total = 0; while (have_posts()) : the_post(); $postmeta = get_post_custom($post->ID); $total += (int) $postmeta['wii-time'][0]; endwhile; echo "<p class='stats-number'>" . formatTime($total) . "</p>"; ?> <p class="stats-text" id="exercise-text">total exercise time</p> </div>
Thanks!
–nks
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Getting custom fields from all posts within a post’ is closed to new replies.