2017-01-23 42 views
0

請有人可以告訴我我的代碼錯了嗎?雖然循環與陣列推get_field ACF插件不能正常工作

循環不起作用。

當我這樣做時,整個頁面都是空白的。

function filter_reports() { 
    global $customer_account; 
    $args = array(
    'post_type' => 'ebooks', 
    'tax_query' => array(
     'relation' => 'AND', 
     array(
     'taxonomy' => 'customer', 
     'field' => 'term_id', 
     'terms' => $customer_account, 
     ), 
     array(
     'taxonomy' => 'disease', 
     'field' => 'term_id', 
     'terms' => $_POST['options'], 
     ) 
    ), 
    ); 

$the_query = new WP_Query($args); 
$results = array(); 

    if ($the_query->have_posts()) { 
    while ($the_query->have_posts()) { 
    $id = get_the_ID(); 
     array_push($results, array(
     'id' => $id, 
     'title' => get_field('title', $id), 
     'chair' => get_field('e-chair', $id), 
    )); 
      } 
    } 

    echo json_encode($results); 
    die; 

} 
    add_action('wp_ajax_filter_reports', 'filter_reports'); 
    add_action('wp_ajax_nopriv_filter_reports', 'filter_reports'); 

我想讓我在ACF插件中製作的自定義字段在我的while循環中循環。 但是整個WHILE不工作。

我真的希望有人能幫助我。

+0

我想(不是一個WP專家),你寫了一個無限的while循環那裏 – RiggsFolly

+0

我該怎麼辦?我陷入困境。 – Dionoh

+0

在實際嘗試使用結果集的while循環中使用調用,而不是隻詢問結果集中是否有任何內容的調用 – RiggsFolly

回答

2

您while循環不工作,因爲你沒有重複的循環,它必須由 the_post() while循環中設置後 指數。

所以,你的代碼應該是這個樣子:

$the_query = new WP_Query($args); 
$results = array(); 
while ($the_query->have_posts()) : $the_query->the_post(); 
    $id = get_the_ID(); 
    array_push($results, array(
     'id' => $id, 
     'title' => get_field('title', $id), 
     'chair' => get_field('e-chair', $id), 
    )); 
endwhile; 
wp_reset_postdata(); 

替代方法:

$the_query = new WP_Query($args); 
$results = array(); 
if (!empty($the_query->posts)) 
{ 
    foreach ($the_query->posts as $post) 
    { 
     $id = $post->ID; 
     array_push($results, array(
      'id' => $id, 
      'title' => get_field('title', $id), 
      'chair' => get_field('e-chair', $id), 
     )); 
    } 
} 
wp_reset_postdata(); 

希望這有助於!

+0

它確實有幫助,但仍然沒有循環,所以我會爲此提出另一個問題 – Dionoh