【发布时间】:2014-02-28 15:42:33
【问题描述】:
所以我不明白为什么这不起作用。我正在使用 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 警告。
警告:非法字符串偏移 '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
)
)
【问题讨论】:
-
蹩脚的黑客:
if(!isset($link['text'])) { $link['text'] = ''; } -
那么循环内的
print_r($links)或print_r($link)给了你什么? -
@Scott 这不是问题所在。问题是它为什么会抛出这个错误。
-
尝试
$k = array_keys($link); var_dump($link);看看是否为每个循环定义了$link['text']。
标签: php wordpress loops foreach advanced-custom-fields