Skip to content
Advertisement

How to solve PHP ACF error: “Trying to access array offset on value of type bool”?

I’ve been working on developing my own theme and had to make a few custom blocks with ACF using my Laravel Sage environment.

My objective is to retrieve a list of text from a repeater object. (which successfully works, but the error is still displayed).

So the code that is throwing that error is as follows:

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

And then the PHP code that’s responsible for creating the repeater blocks is as follows in 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()

Thank you in advance to anyone that’s taking the time to read through this.

Advertisement

Answer

You need an if statement before the while

so you want to do something like this.

@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
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement