尝试将wp_posts中的几个WordPress表行转换为.json文件。不是我见过的JSON API插件很多次。我试过了,它产生了太多的数据。我只想要ID,标题和帖子或页面内容。而已。此外,JSON API实际上不会导出.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":[]}
,就是这样。服务器上有数据。那为什么我不能把它拉进来?有什么问题?
答案 0 :(得分: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);
}
我认为应该是:
while($getdata=mysql_fetch_array($query))
{
$id=$getdata[0];
$title=$getdata[1];
$content=$getdata[2];
$qa[]=array('id'=> $id, 'title'=> $title, 'content'=> $content);
}
因为否则$getdata
为空。
答案 1 :(得分:0)
为什么不使用WP API插件并以这种方式使用数据?