• Hello all,

    I’m very new to WordPress, so I hope this isn’t terribly easy and I’m just being foolish, but I can’t figure it out.

    I’d like to add a custom field in each post with a number, then have a loop on the front page that goes through all the posts and gets all those fields and adds them.

    Example:

    /*Run Through Posts*/
    Post #1: apples = 1
    Post #2: apple = 3
    Post #3: apples = 0
    Post #4: apples = -2
    /* Front Page*/
    Total Apples: 2

    I’m assuming this is possible, but can someone point me in the right direction?

    I’ll continue to work on this and update the forum if I come up with a solution.

    Thanks!

    –Nick

Viewing 4 replies - 1 through 4 (of 4 total)
  • you need to add something inside your loop like $applestotal + $apples where $apples is your custom field value, this will add to $applestotal every time the loop runs through

    Not tested, but something like this should work:

    <?php
    $total = 0;
    while (have_posts()) : the_post();
        $postmeta = get_post_custom($post->ID);
        echo "Apples = {$postmeta['apples'][0]}";
        $total += (int) $postmeta['apples'][0];
    endwhile;
    echo "Total Apples: $total";
    ?>
    Thread Starter enkayes

    (@enkayes)

    +5 internets to you both! Thank you!

    If you implement this on a live site, please give us the link. I’d be interested to see it.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Finding and adding custom fields together’ is closed to new replies.