【问题标题】:ACF: Obtaining info in vars from nested groupACF:从嵌套组中获取 vars 中的信息
【发布时间】:2021-04-08 11:39:39
【问题描述】:

我有以下字段设置:

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"
  }
}

【问题讨论】:

  • 请在$welcome_screen = get_field('welcome_screen'); 之后添加echo '&lt;pre&gt;'; var_dump($welcome_screen); echo '&lt;/pre&gt;'; 并分享输出的转储数组。
  • 嗨@joshmoto - 当然:) 我已经编辑了我的问题以展示输出
  • 为什么不正常循环遍历数组?
  • 如果您试图在while 循环之外获取$terms_text,似乎没有理由在这里使用循环。为什么不使用数组键直接访问数据?看来您甚至没有使用循环来迭代多个数组。

标签: php wordpress advanced-custom-fields


【解决方案1】:

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

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

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

在代码中查看 cmets...

<?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'];

  }

}

【讨论】:

    猜你喜欢
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 2021-11-05
    • 1970-01-01
    相关资源
    最近更新 更多