首页 > 解决方案 > 如何解决 PHP ACF 错误:“尝试访问 bool 类型值的数组偏移量”?

问题描述

我一直在开发自己的主题,不得不使用我的 Laravel Sage 环境使用 ACF 制作一些自定义块。

我的目标是从转发器对象中检索文本列表。(成功工作,但仍然显示错误)。

因此,引发该错误的代码如下:

page.blade.php:

@php
  $tv_lists = [];
  while (have_rows('tv_lists')) {
    the_row();
    $tv_lists[] = [
      'tv_list' => get_sub_field('tv_list'),
    ];
  }
@endphp

@if(get_field('tv_lists'))
          <div class="tv-lists col-12 col-md-8 col-lg-9 block-image">
            @foreach($tv_lists as $tv_list)
              @if(!empty($tv_list['tv_list']))
                <div class="tv-list quote-content">
                  <a class="tv-list-text">{{ $tv_list['tv_list'] }}</a>
                </div>
              @endif
            @endforeach
          </div>
        @endif

然后负责创建中继器块的 PHP 代码在 page.php 中如下所示:

->addRepeater('tv_lists', [
    'conditional_logic' => [[['field' => 'links_type', 'operator' => '==', 'value' => 'list']]],
    'label' => 'TV Lists',
    'layout' => 'block',
  ])
    ->addText('tv_list', [
      'label' => 'TV List',
    ])
    ->endRepeater()

提前感谢任何花时间阅读本文的人。

标签: phpwordpresslaravelroots-sage

解决方案


你需要一个if声明之前while

所以你想做这样的事情。

@php
  $tv_lists = [];
  if (have_rows('tv_lists'){
    while (have_rows('tv_lists')) {
      the_row();
      $tv_lists[] = [
        'tv_list' => get_sub_field('tv_list'),
      ];
    }
  }
@endphp

推荐阅读