douna4762 2015-12-30 23:59
浏览 72

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

Ok, my long day is catching up with me... I am trying to list out custom post meta fields and keep getting Notice: Undefined index: related-headline in... Here is what the array looks like:

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 ) ) ) 

Here is my code:

$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;
}

I know I am missing something simple here so any help would be most appreciated.

  • 写回答

1条回答 默认 最新

  • doujiao1948 2015-12-31 00:32
    关注

    I got it to work by simply adding TRUE to the function call as suggested in the comments by cale_b:

    $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>";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件