2016-11-22 46 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; 
} 
相关问题