计算get_post_meta的倍数$ var

时间:2014-11-13 23:30:55

标签: php arrays wordpress meta-boxes

我是PHP的新手并感谢任何帮助。我试图理解为什么$技能不适用于Wordpress上的get_post_meta。

我正在尝试计算(求和)每个元字段中的所有元数字='ecpt_editorial','ecpt_branding'等等(即='ecpt_editoral'在一个帖子中有3个点,在另一个帖子中有4个点。 ) 我正在尝试计算所有这些而不需要为每个人创建一个$(它们太多了)。

我的错误:
警告:在isset中为非法偏移类型或在中为空 致命错误:

中不支持的操作数类型
 <?php $args = array( 'numberposts' => -1, 'post_type'   => 'post',);
        $points = get_posts( $args );   
        $total = 0;

        $skill = array ('ecpt_editorial','ecpt_branding', 'ecpt_packaging');

        foreach( $points as $point ) {
            $single = get_post_meta( $point->ID, $skill, false );
            $total += $single;}

       echo $total;
       ++$total;
        ?>

1 个答案:

答案 0 :(得分:0)

您无法将数组传递给$ key字段。这是您正在寻找的问题:

    $args = array( 'numberposts' => -1, 'post_type'   => 'post',);
    $points = get_posts( $args );   
    $total = 0;

    $skill = array ('ecpt_editorial','ecpt_branding', 'ecpt_packaging');

    foreach( $points as $point ) {
        foreach($skill as $key){
             $single = get_post_meta( $point->ID, $key, false );
             var_dump($single);
             echo '<br>';
            ${$key} += (int) $single;
        }
   }

   foreach($skill as $key){
       echo $key.'='.${$key}; // there are now 3 variables with count values set, $ecpt_editorial , $ecpt_branding, $ecpt_packaging
   }

假设您不想保存$ single使用?