web-dev-qa-db-fra.com

Comment calculer le total des valeurs des champs personnalisés dans les types d'articles personnalisés?

Donc, le titre n'est pas clair, mais voici ce que j'ai:

Type de message personnalisé: Projets
Type de message personnalisé: Investissements
+ Champ personnalisé: montant

Et j’ai une page où je charge les investissements + les montants dans une boucle de type post personnalisée, qui ressemble donc à ceci: Tableau:

____________________________________________
| custom post type 1 |      amount 1       |
|____________________|_____________________|
| custom post type 2 |      amount 2       |
|____________________|_____________________|

Maintenant, ce que je veux faire est de calculer le montant total du montant 1 + montant 2

au fait, le code que j'utilise est:

<?php
                    if ( is_user_logged_in() ):
                        global $current_user;
                        get_currentuserinfo();

                        $author_query = array('posts_per_page' => '-1','author' => $current_user->ID, 'post_type' => 'investeringen');
                        $author_posts = new WP_Query($author_query);
                        while($author_posts->have_posts()) : $author_posts->the_post();

                        $parent_id = wpcf_pr_post_get_belongs(get_the_ID(), 'project');
                        $parent = get_post($parent_id);
                        $percentagenumber = do_shortcode("[types field='rente' id='$parent_id']");

                        $geinvesteerd = types_render_field("geinvesterd", array( "raw" => "true") );
                        $opgebracht = $geinvesteerd / 100 * $percentagenumber; // 

                ?>
                <tr>
                    <td class="project">
                        <?php
                            $parent_id = wpcf_pr_post_get_belongs(get_the_ID(), 'project');
                                if (!empty($parent_id)) {
                                    $parent = get_post($parent_id);
                                    echo $parent->post_title;
                            }
                        ?>
                    </td>
                    <td class="demo">&euro; <?php echo number_format("$geinvesteerd",0,",","."); ?>,-</td>
                    <td>&euro; <?php echo number_format("$opgebracht",0,",","."); ?>,-</td>
                    <td>
                    <?php
                        echo number_format("$percentagenumber", 2, '.', '') . ' &#37;'; 
                    ?>
                    </td>
                    <td><a href="
                    <?php
                        $parent_id = wpcf_pr_post_get_belongs(get_the_ID(), 'project');
                            if (!empty($parent_id)) {
                                $parent = get_post($parent_id);
                                echo post_permalink($parent_id);
                        }
                    ?>
                    "><img src="<?php echo get_stylesheet_directory_uri(); ?>/images/meta_icons/eye.png" /></a></td>
                </tr>
            <?php
                global $count;
                $count = $author_posts->post_count;
                endwhile;

                else :
            ?>  
                <tr>
                    <td colspan="5" style="text-align:left">U moet inloggen voordat u deze pagina / statistieken kunt bekijken</td>
                </tr>
            <?php
                endif;
            ?>

Merci d'avoir lu, j'espère que vous pourrez m'aider!

1
Cas

C'est une question de garder un total cumulé. Parce que je ne suis pas sûr à 100% de la question, je fournis ci-dessous les modifications/pseudo-codes de ce que vous voulez faire:

<?php
                if ( is_user_logged_in() ):
                    global $current_user;
                    get_currentuserinfo();

                    // ADDED: Initialize the total variable to prevent warnings
                    $grandtotal = 0;

                    $author_query = array('posts_per_page' => '-1','author' => $current_user->ID, 'post_type' => 'investeringen');
                    $author_posts = new WP_Query($author_query);
                    while($author_posts->have_posts()) : $author_posts->the_post();

                    $parent_id = wpcf_pr_post_get_belongs(get_the_ID(), 'project');
                    $parent = get_post($parent_id);
                    $percentagenumber = do_shortcode("[types field='rente' id='$parent_id']");

                    $geinvesteerd = types_render_field("geinvesterd", array( "raw" => "true") );
                    $opgebracht = $geinvesteerd / 100 * $percentagenumber; //

                    // ADDED: Add this to the running total
                    $grandtotal+= $geinvesteerd; 

            ?>
            <tr>
                <td class="project">
                    <?php
                        $parent_id = wpcf_pr_post_get_belongs(get_the_ID(), 'project');
                            if (!empty($parent_id)) {
                                $parent = get_post($parent_id);
                                echo $parent->post_title;
                        }
                    ?>
                </td>
                <td class="demo">&euro; <?php echo number_format("$geinvesteerd",0,",","."); ?>,-</td>
                <td>&euro; <?php echo number_format("$opgebracht",0,",","."); ?>,-</td>
                <td>
                <?php
                    echo number_format("$percentagenumber", 2, '.', '') . ' &#37;'; 
                ?>
                </td>
                <td><a href="
                <?php
                    $parent_id = wpcf_pr_post_get_belongs(get_the_ID(), 'project');
                        if (!empty($parent_id)) {
                            $parent = get_post($parent_id);
                            echo post_permalink($parent_id);
                    }
                ?>
                "><img src="<?php echo get_stylesheet_directory_uri(); ?>/images/meta_icons/eye.png" /></a></td>
            </tr>
        <?php
            global $count;
            $count = $author_posts->post_count;
            endwhile;

            // ADDED: Output the grand total here
            ?>
            <tr><td>TOTAL</td><td><?php echo number_format($grandtotal, 0, ",", "."); ?></td><td colspan="3">&nbsp;</td></tr>
            <?php
            else :
        ?>  
            <tr>
                <td colspan="5" style="text-align:left">U moet inloggen voordat u deze pagina / statistieken kunt bekijken</td>
            </tr>
        <?php
            endif;
        ?>
1
cale_b