【发布时间】:2014-05-16 12:52:59
【问题描述】:
我正在尝试从我的 wordpress 页面制作自定义 json 提要。问题是循环似乎覆盖了每个对象,所以它只将最后一个对象打印为 json。我也试过在循环内移动回声,但它看起来可能在浏览器中没有格式化。
这是我的代码:
foreach ($posts as $post) {
$r = str_replace("\n",'', shorten_txt($post->post_content, 500));
$n = str_replace("\r", '', $r);
$post_data = array(
'title' => get_the_title($post->ID),
'link' => get_permalink($post->ID),
'image' => catch_that_image(),
'content' => $n,
'time' => strtotime($post->post_date_gmt));
$data = (array('item' => $post_data));
}
echo json_encode($data);
【问题讨论】: