2016-10-24 30 views
0

在那裏我想顯示我的數據。當我嘗試不postmeta其工作,以顯示我的數據,但是當我試圖用postmeta改善我得到的空白響應空白的迴應,當我想顯示postmeta WordPress的

$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

$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; 
} 
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; 
} 
+0

未定義索引時沒有結果。 – Walf

+0

我不知道wordpress,但我在他的發言中看到了一個失蹤的「endif」。由於我的低點,我不能直接添加評論問題。 OP必須瞭解如何在第一時間啓用錯誤調試 –

+0

我已經更新了我的答案..我真的不知道對那些自定義定義的wordpress方法有什麼期望,所以$ data []可以在while子句之內或之外。我試圖修復語法 –

-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);