首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从CPT中的子字段中获取合计

问从CPT中的子字段中获取合计
EN

Stack Overflow用户
提问于 2019-08-19 13:14:56
回答 2查看 41关注 0票数 0

我试图在自定义post类型的可重复子域中收集一系列数字,然后将总和输出到一行文本中。我已经写了下面的代码,但我在让它工作方面取得的成功很少。

如果有人能为我的教育看一下并修改一下,我将不胜感激。谢谢

代码语言:javascript
复制
<?php
  $args = array( 'post_type' => 'distribution-post' );
  $amount = new WP_Query( $args );
  $total = 0;

  while ( have_rows('arr_dist') ) : the_row();
      $total += intval( get_sub_field('amount'));
  endwhile;

  echo $total;
  wp_reset_query();
?>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-08-19 13:43:06

尝试下面的代码。在使用have_row之前,您已经错过了使用have_post。

代码语言:javascript
复制
$args = array( 'post_type' => 'distribution-post' );
  $amount = new WP_Query( $args );
  $total = 0;

  if ( $amount->have_posts() ) {
        while ( $amount->have_posts() ) : $amount->the_post(); 
            while ( have_rows('arr_dist') ) : the_row();
              $total += intval( get_sub_field('amount'));
            endwhile;
        endwhile;
        wp_reset_postdata();
    }

  echo $total;
  wp_reset_query();
票数 0
EN

Stack Overflow用户

发布于 2019-08-19 14:21:10

​

代码语言:javascript
复制
<?php 
$total = 0;
while(the_repeater_field('repeaterfield name' )): 
							  
$total += get_sub_field('repeatersubfield name' );
							  
endwhile; 
echo $total;
?>

​

根据需要更改中继器字段和子字段。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57550805

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档