从 ACF 组中获取字段作为 vars

Obtaining fields from within ACF group as vars

我正在尝试从 ACF 获取值 group 但无法通过两种方法获取它们:

下面是我的ACF fields设置方式,供参考:

方法一:

<?php

$welcome_screen_content = get_field('welcome_screen_content'); // type: group

if( $welcome_screen_content ):
  $title = get_field('title');
endif;


echo $title; // prints nothing

?>

方法二:

<?php

if( have_rows('welcome_screen_content') ):
  while( have_rows('welcome_screen_content') ): the_row();

  $title = get_sub_field('title');

  echo $title; // prints nothing

  endwhile;
endif;

?>

在我的 post 模板上,title 确实有一个值:

不确定为什么在两次尝试中,回显 $title 什么也没做?

group 上使用 get_field 时,高级自定义字段 returns 包含组字段的关联数组。因此,要获得标题,您可以这样做:

$welcome_screen_content = get_field('welcome_screen_content'); // type: group

if( $welcome_screen_content ):
  $title = $welcome_screen_content['title'];
endif;

方法 1 未按预期工作,因为 get_field('title') 要求 ACF 获取与“welcome_screen_content”组相同级别的字段。

方法 2 未按预期工作,因为 get_subfield 用于 Repeater or Flexible Content Fields