2014-05-16 5 views
-1

내 WordPress 페이지에서 사용자 정의 json 피드를 만들려고합니다. 문제는 루프가 모든 객체를 덮어 쓰는 것처럼 보이므로 마지막 객체 만 json으로 인쇄합니다. 루프 내에서 메아리를 움직이려고했지만 브라우저에서 포맷되지 않은 것 같습니다. $data의 선언은 매번 다시 초기화됩니다 값이 할당되어 의미의 foreach 내에서 무슨 일이 일어나고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); 

답변

0

:

여기 내 코드입니다. 실수로이 작업을 수행했는지 또는이 작업을 수행하는 방법을 잘 모르는 경우 확실하지 않습니다.

이 인이 무엇

$data = array(); 
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); 

사용해 그것을 foreach 루프의 외부 $data 변수를 선언하고, 할당이 배열로 푸시된다. 희망이 도움이됩니다.

+0

감사합니다. 내가 할 수있을 때 받아 들일거야. – user3423384

관련 문제