Ich arbeite an WordPress JSON API-Plugin. Ich meine benutzerdefinierte Controller erstellt, möchte ich Beiträge als JSON-Format, meine Arbeit war, wie unten abgerufen werden:
Mein PHP-Code ist:
$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 Ergebnis wie folgt:
{
"status": "ok",
"count": 10,
"result": [
{
"id": "51",
"title": "a",
"price": null
},
{
"id": "82",
"title": "b",
"price": null
},
}
Warum Preis ist auf Null gesetzt, was die richtige Syntax, um Preis von benutzerdefinierten Feldern aus Posts in WordPress zu extrahieren?
Bitte formatieren Sie Ihren Code richtig. –
Machen Sie eine 'print_r ($ rows)', und sehen Sie, was Sie innen haben. Aus Ihrem Ergebnis scheint $ post-> custom_fields-> price '' null' zu sein ... –