提问人:Joe Conlin 提问时间:12/31/2015 最后编辑:Joe Conlin 更新时间:12/31/2015 访问量:260
将 foreach 与 Wordpress 一起使用的未定义索引
Undefined index using foreach with Wordpress
问:
好吧,我漫长的一天正在赶上我......我正在尝试列出自定义帖子元字段并不断收到通知:未定义的索引:相关标题...数组如下所示:
Array ( [0] => Array ( [0] => Array ( [related-headline] => Street Outlaws Turbo Rotary Mazda RX-7 – OKC NoPrep [related-url] => //localhost:3000/street-outlaws-turbo-rotary-mazda-rx-7-okc-noprep/ [related-image_id] => 78055 [related-image] => //localhost:3000/wp-content/uploads/2015/12/lucky-to-be-alive-distracted-dri.jpg ) [1] => Array ( [related-headline] => In the Driver’s Seat: Shane vs. The Reaper | Street Outlaws [related-url] => //localhost:3000/in-the-drivers-seat-shane-vs-the-reaper-street-outlaws/ [related-image_id] => 78048 [related-image] => //localhost:3000/wp-content/uploads/2015/12/ford-f350-vs-dodge-ram-vs-chevy.jpg ) ) )
这是我的代码:
$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;
}
我知道我在这里缺少一些简单的东西,所以任何帮助将不胜感激。
答:
0赞
Joe Conlin
12/31/2015
#1
我通过简单地按照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>";
}
评论
Array ( [0] => Array ( [0] => Array (
;(array) $entries
->(array) $entries[0]
foreach ( $entries[0] as $key => $entry ) {
$entries = get_post_meta( get_the_ID(), 'ss-related-posts' );
$entries = get_post_meta( get_the_ID(), 'ss-related-posts' , TRUE);