2013-09-03 3 views
1

WordPress 테이블 행을 wp_posts에서 .json 파일로 변환 해보십시오. 내가 여러 번 게시 한 JSON API 플러그인이 아닙니다. 나는 그것을 시도하고 너무 많은 데이터를 생성합니다. ID, 제목 및 게시물 또는 페이지 콘텐츠 만 있으면됩니다. 그게 전부 야. 또한 JSON API는 실제로 .json 파일을 내 보내지 않습니다. 내가 서버에서 동적으로 원한다.WordPress에서 .json 파일 .... 데이터가 나타나지 않습니다.

그래서이 스크립트를 사용하여 .json 파일을 생성합니다.

<?php $con=mysql_connect("localhost","username","password") or die("Database connection failed"); 

if($con) 
{ 
mysql_selectdb("database_wp",$con); 
} 
?> 

<?php 

$data=array(); 
$qa=array(); 
$query=mysql_query("SELECT id, title, content FROM wp_posts ORDER BY id"); 
while($geteach=mysql_fetch_array($query)) 
{ 
$id=$getdata[0]; 
$title=$getdata[1]; 
$content=$getdata[2]; 
$qa[]=array('id'=> $id, 'title'=> $title, 'content'=> $content); 
} 
$data['qa']=$qa; 

$fp = fopen('myjson.json', 'w'); 
fwrite($fp, json_encode($data)); 
fclose($fp); 

?> 

결과로 얻은 .json 파일은 모두 {"qa":[]}이며 그게 전부입니다. 서버에 데이터가 있습니다. 그럼 왜 못 가져 가니? 그 수정은 뭐니?

답변

1

나는 while 루프에 문제가 있다고 생각합니다.

당신은 내가 그것을해야한다 생각

while($geteach=mysql_fetch_array($query)) 
{ 
    $id=$getdata[0]; 
    $title=$getdata[1]; 
    $content=$getdata[2]; 
    $qa[]=array('id'=> $id, 'title'=> $title, 'content'=> $content); 
} 

를 사용하십시오, 그렇지 않으면 $getdata가 비어

while($getdata=mysql_fetch_array($query)) 
{ 
    $id=$getdata[0]; 
    $title=$getdata[1]; 
    $content=$getdata[2]; 
    $qa[]=array('id'=> $id, 'title'=> $title, 'content'=> $content); 
} 

때문입니다.

0

왜 WP API 플러그인을 사용하지 않고 그런 방식으로 데이터를 사용합니까?

관련 문제