如何计算自定义帖子类型中自定义字段的值的总和?

时间:2013-08-07 作者:Cas

所以标题有点不清楚,但我有:

自定义贴子类型:项目自定义贴子类型:投资      ;+自定义字段:金额

我有一个页面,在其中我将投资+金额加载到一个自定义的post类型循环中,所以它看起来像这样:表:

____________________________________________
| custom post type 1 |      amount 1       |
|____________________|_____________________|
| custom post type 2 |      amount 2       |
|____________________|_____________________|
现在我要做的是计算金额1+金额2的总额

顺便说一句,我使用的代码是:

<?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;
            ?>
谢谢你们的阅读,希望你们能帮助我!

1 个回复
最合适的回答,由SO网友:random_user_name 整理而成

这是一个保持总数的问题。因为我对这个问题不是百分之百清楚,下面我将提供您想要做的更改/伪代码:

<?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;
        ?>

结束

相关推荐

在jQuery中,使用php变量来执行排队jQuery文件

我正在一个特定的Wordpress管理页面上加载一些jquery,jquery可以加载并正常工作,但如果存在php变量,我只需要这个jquery触发器。在php中,我只需编写一个函数并将其挂接到管理页面:我正在将jquery文件排队,即foo。包含以下jquery的js:jQuery(document).ready(function($){ $(\'#someID.some-class > a\').append(\'<div class=\"caption\"><