2016-11-22 25 views
0

我使用選項頁面中的ACF字段來填充重力形式下拉列表。這些值從字段中拉出,但顯示在表單頂部(預覽中),而不是下拉列表中。我確信這是我構建表單的一個問題。提前感謝您的幫助!重力形式動態人口和高級自定義字段,選項不顯示

參見Screenshot

//gravity forms dynamically populate from ACF 

add_filter('gform_pre_render_1', 'populate_posts'); 
add_filter('gform_pre_validation_1', 'populate_posts'); 
add_filter('gform_pre_submission_filter_1', 'populate_posts'); 
add_filter('gform_admin_pre_render_1', 'populate_posts'); 

function populate_posts($form) { 

    global $choices; 

    foreach ($form['fields'] as &$field) { 

     if ($field->type != 'select' || strpos($field->cssClass, 'populate-posts') === false) { 
      continue; 
     } 

     if(have_rows('core_values', 'option')): 

      while(have_rows('core_values', 'option')): the_row(); 
       $choices[] = array('text' => the_sub_field('value_mstr_name'), 'value' => the_sub_field('value_mstr_name')); 
      endwhile; 


     // update 'Select a Post' to whatever you'd like the instructive option to be 
     $field->placeholder = 'Select a Post'; 
     $field->choices = $choices; 

     endif; 

    } 

    return $form; 
} 

回答

0

我沒有定義的選擇作爲陣列和用於ACF,功能the_sub_field就像sub_field的回聲,但實際上get_sub_field傳播入GF域對象的選擇屬性。

//gravity forms dynamically populate from ACF 

add_filter('gform_pre_render_1', 'populate_posts'); 
add_filter('gform_pre_validation_1', 'populate_posts'); 
add_filter('gform_pre_submission_filter_1', 'populate_posts'); 
add_filter('gform_admin_pre_render_1', 'populate_posts'); 

function populate_posts($form) { 

    foreach ($form['fields'] as &$field) { 

     if ($field->type != 'select' || strpos($field->cssClass, 'populate-posts') === false) { 
      continue; 
     } 

     if(have_rows('core_values', 'option')): 

     $choices = array(); 

      while(have_rows('core_values', 'option')): the_row(); 
       $choices[] = array('text' => get_sub_field('value_mstr_name'), 'value' => get_sub_field('value_mstr_name')); 
      endwhile; 


     // update 'Select a Post' to whatever you'd like the instructive option to be 
     $field->placeholder = 'Select a Core Value'; 
     $field->choices = $choices; 

     endif; 

    } 

    return $form; 
} 
相關問題