我有一个Wordpress Gravity Form。
在表单的第一页上,我有一个动态填充的下拉框。这里没问题。
现在,在相同表单的第二页上,我还有另一个下拉列表,我需要根据第一个下拉列表中的选定选项动态填充。
这可能吗?如果可以的话,不会太复杂吗?
答案 0 :(得分:1)
以下是我根据第1页下拉菜单中选择的帖子填充第2页的下拉菜单的示例:
add_filter( 'gform_pre_render_918', 'populate_post_meta' );
add_filter( 'gform_pre_validation_918', 'populate_post_meta' );
add_filter( 'gform_pre_submission_filter_918', 'populate_post_meta' );
add_filter( 'gform_admin_pre_render_918', 'populate_post_meta' );
function populate_post_meta( $form ) {
$field_id = 3;
$post_field_id = 1;
foreach ( $form['fields'] as &$field ) {
if ( $field->id != $field_id ) {
continue;
}
// you can add additional parameters here to alter the posts that are retrieved
// more info: [http://codex.wordpress.org/Template_Tags/get_posts](http://codex.wordpress.org/Template_Tags/get_posts)
$post_id = rgpost( "input_{$post_field_id}" );
$post_meta = get_post_custom( $post_id );
$choices = array();
foreach ( $post_meta as $value ) {
$choices[] = array( 'text' => $value[0], 'value' => $value[0] );
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select a Value';
$field->choices = $choices;
}
return $form;
}