使用带有Wordpress的foreach的未定义索引

时间:2015-12-30 23:59:26

标签: php arrays wordpress foreach undefined-index

好的,我漫长的一天正赶上我......我正在尝试列出自定义的帖子元字段并不断收到通知:未定义的索引:相关标题... 这是数组的样子:

$entries = get_post_meta( get_the_ID(), 'ss-related-posts' );
// print_r($entries);

foreach ( (array) $entries as $key => $entry ) {
    $title = '';
    $title = $entry['related-headline'];
    echo $title;
}

这是我的代码:

{{1}}

我知道我在这里遗漏了一些简单的东西,所以任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:0)

我按照comments by cale_b中的建议,只需将TRUE添加到函数调用中即可实现:

$entries = get_post_meta( get_the_ID(), 'ss-related-posts', TRUE );

foreach ( (array) $entries as $key => $entry ) {
    $title = $url = $img = "";
    $title = $entry['related-headline'];
    $url = $entry['related-url'];
    $img = $entry['related-image'];

    echo $title . "<br>" . $url . "<br>" . $img . "<br>";
}