1
我正在使用WordPress json API插件。我创建了自定义的控制,我想要检索的职位为JSON格式,我的工作是如下:
我的PHP代码:
$rows = $wpdb->get_results($wpdb->prepare("select * from $wpdb->posts where $wpdb->posts.post_status = 'publish' AND
$wpdb->posts.post_title LIKE '%%%s%%' ", $key));
$count = 0 ;
foreach($rows as $post) {
$output[] = array('id' => $post->ID, 'title' => $post->post_title, 'price' =>$post->custom_fields->price);
++$count ;
}
if($count == 0){
$data = array ('status'=>'ok', 'count'=> $count , 'result'=> "No data found ");
}else
{
$data = array ('count'=> $count , 'result'=> $output);
}
header('Content-Type: application/json; charset=UTF-8');
return $data;
}
JSON结果如下:
{
"status": "ok",
"count": 10,
"result": [
{
"id": "51",
"title": "a",
"price": null
},
{
"id": "82",
"title": "b",
"price": null
},
}
为什么价格设置为空,什么正确的语法从WordPress中的帖子的自定义字段中提取价格?
请正确格式化您的代码。 –
做一个'print_r($ rows)',看看里面有什么。从你的结果'$ post-> custom_fields-> price'似乎是'null' ... –