所以我不明白为什么这不起作用。我正在使用 Wordpress 和一个名为 ACF 的插件来填充一些数据。我正在循环转发器字段以吐出数据,如下所示:
<?php
$links = get_field('footer_links'); // spits out the array
if($links) {
foreach ( $links as $link ) {
$logo = $link['logo'];
$link = $link['link'];
$text = $link['text'];
echo '<div class="link">';
echo ' <a href="'.$link.'"><img src="'.$logo.'" /><p>'.$text.'</p></a>';
echo '</div>';
}
}
// Logo spits out a image path
// link spits out the URL path
// text SHOULD just spit out the title, however throws PHP warning
?>
对于变量 $text
我收到 PHP 警告。
Warning: Illegal string offset 'text' in...
为什么我的其他变量 - $logo
、$link
不会抛出此警告?它们的创建方式与其他 $text
变量相同。
我尝试重新创建整个中继器字段并更改名称等......但没有成功。
这是我的 print_r($links);
Array
(
[0] Array
(
[logo] http://domainname.com/imagepath
[link] http://.....
[text] Text1
)
[1] Array
(
[logo] http://domainname.com/imagepath
[link] http://.....
[text] Text2
)
[2] Array
(
[logo] http://domainname.com/imagepath
[link] http://.....
[text] Text3
)
)
最佳答案
这里抛出错误:
$link['text'];
数组$link
缺少索引['text']
。这是因为您在这里覆盖了数组变量:
$link = $link['link'];
将其更改为:
$href = $link['link'];
你就是黄金。
关于PHP 警告 - 非法字符串偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22099529/