我正在使用选项页面中的ACF字段来填充Gravity Forms下拉列表。值从字段中拉出,但显示在表单的顶部(在预览中),而不是在下拉列表中。我确信这是我构建表单的一个问题。提前感谢您的帮助!
请参阅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 :(得分:0)
我没有将选项定义为ACF的数组AND,函数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;
}
答案 1 :(得分:0)
您还可以将此限制为特定的帖子类型
add_filter( 'gform_pre_render_3', 'populate_warranty_form' );
add_filter( 'gform_pre_validation_3', 'populate_warranty_form' );
add_filter( 'gform_pre_submission_filter_3', 'populate_warranty_form' );
add_filter( 'gform_admin_pre_render_3', 'populate_warranty_form' );
function populate_warranty_form( $form ) {
foreach ( $form['fields'] as &$field ) {
// select field with css class 'product-name'
// this allows dynamic field to use existing css
if ( $field->type != 'select' || strpos( $field->cssClass, 'product-name' ) === false ) {
continue;
}
$args = array(
'post_type' => 'custom_post_type',
'posts_per_page'=> '-1',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC'
);
$posts = get_posts( $args );
foreach ( $posts as $post ) {
$postID = $post->ID;
if( have_rows('repeater_field', $postID) ): while( have_rows('repeater_field', $postID) ): the_row();
$choices[] = array( 'text' => get_sub_field('repeater_sub_field'), 'value' => get_sub_field('repeater_sub_field') );
endwhile; endif;
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select';
$field->choices = $choices;
}
return $form;
}