php - 如何解决 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()
提前感谢任何花时间阅读本文的人。
解决方案
你需要一个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
推荐阅读
- python - 检查数据库是否有新条目最快和最有效的方法是什么?
- node.js - OpenShift - 应用程序打包
- c - 使用多线程将大数相乘
- azure - 插入 SQL 表时出现 Azure Logic App 问题
- postgresql - PostgreSQL 将自身限制为单核 CPU 使用率(调试锁定问题?)
- octave - 绘制内部有孔的多边形
- python - 编译时 Tesseract 失败/错误(nameoffset)
- android - 尝试视频通话时Android Webview Permission denied错误
- r - 如何在 argonDash 仪表板中使用模块
- centos - 为什么 Podman 使用不同的 UID 报告“命名空间中可用的 ID 不足”?