ACF:从嵌套组获取 vars 中的信息

ACF: Obtaining info in vars from nested group

我有以下字段设置:

welcome_screen (type: group)
  title
  terms_group (type: group)
    terms_text (type: text)

我正在尝试获取 terms_text 的值。

这是我目前的情况:

<?php
$welcome_screen = get_field('welcome_screen'); // type: group

if($welcome_screen):
  $title        = $welcome_screen['title'];

  while( have_rows('welcome_screen') ): the_row();
    $terms_group = $welcome_screen('terms_group'); // nested group
    $terms_text           = $terms_group['terms_text'];
  endwhile;

endif;

echo $terms_text;

?>

目前,如果我在 while 循环中 echo $terms_text,我会在这一行收到错误 Function name must be a string$terms_group = $welcome_screen('terms_group');

我还想在循环外使用 $terms_text 变量,所以想知道是否有其他方法可以在没有 while 循环的情况下实现我想要的效果?

编辑:

我有运行一个var_dump来检查输出:

$welcome_screen = get_field('welcome_screen'); // type: group
echo '<pre>'; var_dump($welcome_screen); echo '</pre>';

这是输出:

array(3) {
  ["title"]=>
  string(13) "Health Survey"
  ["standfirst"]=>
  string(113) "We just need some answers to some quick health questions about your general health to get you the best treatment."
  ["terms_group"]=>
  array(3) {
    ["terms_text"]=>
    string(41) "By proceeding you agree to the following:"
    ["terms_listing"]=>
    array(2) {
      [0]=>
      array(1) {
        ["terms"]=>
        string(88) ""
      }
      [1]=>
      array(1) {
        ["terms"]=>
        string(0) ""
      }
    }
    ["agree_to_terms_radio"]=>
    string(3) "Yes"
  }
}

好的,为发布转储数据干杯。

所以我认为您不需要使用循环,您可以尝试下面的 php。它未经测试,因此如果我犯了任何错误,您可能需要修复任何错误。

我不记得如果未设置 acf 是否输出数组值,因此您可能需要将 isset() 添加到某些 php 变量以检查是否已设置。

查看代码中的注释...

<?php

// get welcome screen group
$welcome_screen = get_field('welcome_screen');

// check if we have group
if($welcome_screen) {
  
  // output the welcome title
  echo $welcome_screen['title'];
  
  // set the terms group
  $terms_group = $welcome_screen['terms_group'];

  // if terms group is array
  if(is_array[$terms_group]) {
    
    // output term group term text
    echo $terms_group['terms_text'];

    // set term group term listing
    $terms_listings = $terms_group['terms_listing'];
    
    // if terms listings is an array
    if(is_array($terms_listings)) {
      
      // foreach listings
      foreach($terms_listings as $terms_listing) {
        
        // output listing terms
        echo $terms_listing['terms'];

      }

    }

    // output term group term agreement
    echo $terms_group['agree_to_terms_radio'];

  }

}