我正在尝试添加来自不同帖子的自定义字段。我创建的数组不工作。
这是代码。
<?php $totalpricearray = query_posts(\'post_type=items&author=\'.$thisauthorID.\'&tag=\'.$thispostID); while (have_posts()) : the_post();
$productprice = get_post_meta($post->ID, "productprice", true);
$productquantity = get_post_meta($post->ID, "productquantity", true);
$totalproductprice = ($productprice * $productquantity);
echo $totalproductprice, \',\';
endwhile;
$totalprice = array($totalpricearray);
echo array_sum($totalprice); ?>
任何想法,
非凡的
(ps刚刚注意到数组正在工作,但等于0)
最合适的回答,由SO网友:wyrfel 整理而成
<?php
$totalprice_posts = get_posts(\'post_type=items&author=\'.$thisauthorID.\'&tag=\'.$thispostID.\'&numberposts=-1\');
$totalprice_array = array();
foreach ($totalprice_posts as $post) {
$productprice = get_post_meta($post->ID, "productprice", true);
$productquantity = get_post_meta($post->ID, "productquantity", true);
$totalproductprice = ($productprice * $productquantity);
array_push($totalprice_array, $totalproductprice);
}
echo implode(\',\', $totalprice_array);
echo array_sum($totalprice_array);
?