在那里我想显示我的数据。当我试图显示我的数据而没有postmeta它的工作,但当我尝试改进postmeta我得到空白的回应
$command = $_GET['command'];
switch ($command) {
case 'list_product':
$loop = new WP_Query(
array(
'post_type' => 'product'
// 'showposts' => 4,
// 'meta_key' => '_sale_price',
// 'meta_value' => '0',
// 'meta_compare' => '>=',
)
);
if($loop->have_posts()) :
$data = array( "api_status" => 1, "api_message" => "success");
while ( $loop->have_posts() ) : $loop->the_post();
$data[] = array("id" => get_the_ID(),
"post_name" => get_the_title(),
"post_meta" => get_post_meta(get_the_ID());
endwhile;
echo json_encode($data);
break;
}
有人帮助我在我的代码中需要改进,所以我的代码可以像我需要的那样工作吗?
答案 0 :(得分:0)
将$data = ...
放在之前if
,然后添加一个带有空数组值的results
键。将echo
放在endif
之后。
在while
中,使用$data['results'][] = ...
编辑:拼出一点。如果您的结果是空的,则其他错误。
switch ($_GET['command']) {
case 'list_product':
$loop = new WP_Query(
array(
'post_type' => 'product',
// 'showposts' => 4,
// 'meta_key' => '_sale_price',
// 'meta_value' => '0',
// 'meta_compare' => '>=',
)
);
$data = array(
'api_status' => 1,
'api_message' => 'success',
'results' => array(),
);
while ($loop->have_posts()) {
$loop->the_post();
$data['results'][] = array(
'id' => get_the_ID(),
'post_name' => get_the_title(),
'post_meta' => get_post_meta(get_the_ID()),
);
}
echo json_encode($data);
break;
}
答案 1 :(得分:0)
试试这个:
$command = $_GET['command'];
switch ($command) {
case 'list_product':
$loop = new WP_Query(
array(
'post_type' => 'product'
// 'showposts' => 4,
// 'meta_key' => '_sale_price',
// 'meta_value' => '0',
// 'meta_compare' => '>=',
)
);
if($loop->have_posts()) {
$data = array( "api_status" => 1, "api_message" => "success");
while( $loop->have_posts() ) {
$loop->the_post();
$data[] = array(
"id" => get_the_ID(),
"post_name" => get_the_title(),
"post_meta" => get_post_meta(get_the_ID())
);
}
/**
$data[] = array(
"id" => get_the_ID(),
"post_name" => get_the_title(),
"post_meta" => get_post_meta(get_the_ID())
);
*/
}
echo json_encode($data);
break;
}
答案 2 :(得分:-1)
请尝试使用以下代码:
$loop = new WP_Query(
array(
'post_type' => 'post'
)
);
$data = array( "api_status" => 1, "api_message" => "success");
if($loop->have_posts()) {
while( $loop->have_posts() ) {
$loop->the_post();
$data[] = array(
"id" => get_the_ID(),
"post_name" => get_the_title(),
"post_meta" => get_post_meta(get_the_ID())
);
}
}
echo json_encode($data);