0

在那里我想显示我的数据。当我尝试在没有 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;
}

有人帮我我的代码需要什么改进,这样我的代码就可以像我需要的那样工作吗?

4

3 回答 3

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;
}
于 2016-10-24T05:27:59.677 回答
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;
}
于 2016-10-24T05:28:06.010 回答
-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);
于 2016-10-24T08:19:18.840 回答