2014-11-13 27 views
0

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

回答

0

不支持的操作類型你不能傳遞一個陣列到$關鍵領域英寸這可能是你想要的:

$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使用?

+0

嗨大衛! 我不需要$單,但我需要總和所有$單,這是否有道理?我收到錯誤消息:行上不受支持的操作數類型$ total + = $ single; – Bibiana

+0

wierd ....你確定你在所有這些領域都有數字嗎?我更新了一些東西以確保! – David

+0

不,實際上很多領域都是空白的。仍然得到相同的錯誤。讓我試着再次解釋:我在每篇文章中填寫每個'ecpt_x'選項元框。如果它是'0',我什麼都不填。我想總結所有'ecpt_x'metabox中的所有值。像: ecpt_editorial = 2 + 2 + 0 = 4和ecpt_branding = 0 + 2 + 5 = 7。 – Bibiana