简体   繁体   English

PHP 和 Pod:仅将数组值合并到新数组中

[英]PHP and Pods: Combine array values only into new array

Long time user, first time poster.长期用户,第一次发帖。 The forums here have got me out of a lot of trouble but I'm really stuck on this one.这里的论坛让我摆脱了很多麻烦,但我真的坚持这个。

I'm using Pods for a Custom Post Type that has a Custom Field where a user can enter a numeric value.我将 Pods 用于具有自定义字段的自定义帖子类型,用户可以在其中输入数值。 eg.例如。 or 4 or 2 etc或 4 或 2 等

I want to display the total sum of this Custom Field across all user made posts on the front end.我想在前端的所有用户发布的帖子中显示此自定义字段的总和。 To achieve this I'm using a Pods Template to make a short code for the front end, but to do the calculation I'm using PHP.为了实现这一点,我使用 Pods 模板为前端制作一个短代码,但为了进行计算,我使用的是 PHP。

So my current PHP is:所以我现在的 PHP 是:

function jobs_total ($id) {
    $pods = pods ('pledged_job', $id);
    $jobs = ($pods->field ('jobs_pledged'));
    $a = ($jobs);
    $b = explode(' ', $a);

var_dump($b);
}

And the result I get so far is:到目前为止我得到的结果是:

array(1) { [0]=> string(1) "5" } 
array(1) { [0]=> string(1) "4" } 
array(1) { [0]=> string(1) "2" } 
array(1) { [0]=> string(1) "7" }

How do I take the numeric values from "_", which are correctly appearing from post entries, and combine them in a new array so I can perform an 'array_sum' and return the total of those numbers?!如何从帖子条目中正确显示的“_”中获取数值,并将它们组合到一个新数组中,以便我可以执行“array_sum”并返回这些数字的总和?!

I'm a PHP novice so I'm not sure if this is obvious or if it's a clash between Pods terms and standard PHP.我是 PHP 新手,所以我不确定这是否显而易见,或者是否是 Pods 条款与标准 PHP 之间的冲突。

Thanks in advance!!提前致谢!!

Final code wrapped in shortcode to allow for displaying on the frontend through Elementor最终代码包装在简码中,以允许通过 Elementor 在前端显示

function jobs_shortcode () { 
$jobs = get_posts(array( 
'post_type' => 'pledged_job', 
'post_status' => 'publish', 
'numberposts' => -1, )); 

$total = 0; 

foreach ($jobs as $field) { 
$total += (int) get_post_meta($field->ID, 'jobs_pledged', true); 
} 

echo $total; 
} 
add_shortcode( 'jobs', 'jobs_shortcode' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM