获取重力形式的字段值并将该值用作 php 参数?
Get the value of a field in gravity forms and use that value as a php parameter?
我正在尝试在 Gravity Forms 表单中动态填充两个下拉字段。第一个字段动态填充自定义 post 类型中可用的术语。我希望第二个动态填充的字段包含自定义 post 类型中所有 post 标题的列表,并让这些标题按上一个下拉列表中选择的术语进行过滤。是否可以在 Gravity Forms 中获取下拉列表的值并将该值作为参数传递给 $args 以使用 get_posts($args) 函数?
我开始使用以下教程作为指南。 https://docs.gravityforms.com/dynamically-populating-drop-down-fields/
add_filter( 'gform_pre_render_3', 'populate_procedures' );
add_filter( 'gform_pre_validation_3', 'populate_procedures' );
add_filter( 'gform_pre_submission_filter_3', 'populate_procedures' );
add_filter( 'gform_admin_pre_render_3', 'populate_procedures' );
function populate_procedures( $form ) {
// Procedure Category Dropdown
foreach ( $form['fields'] as &$field ) {
第一个字段。以下代码填充一个下拉字段,其中包含自定义 post 类型(过程)中所有术语的列表:
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate_procedure_categories' ) === false ) {
continue;
}
$terms = get_terms( array(
'taxonomy' => 'procedure_category',
'orderby' => 'name',
'order' => 'ASC',
) );
// you can add additional parameters here to alter the posts that are retrieved
// more info: http://codex.wordpress.org/Template_Tags/get_posts
//$posts = get_posts( 'post_type=procedure&numberposts=-1&post_status=publish' );
$choices = array();
foreach ( $terms as $term ) {
$choices[] = array( 'text' => $term->name, 'value' => $term->name );
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select Procedure Category';
$field->choices = $choices;
第二个字段。 以下代码使用自定义 post 类型(过程)的所有 post 标题动态填充该字段。我想根据上面选择的值过滤这些结果。
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate_procedures' ) === false ) {
continue;
}
$args = array(
'post_status' => 'publish',
'post_type' => 'procedure',
'procedure_category' => 'cardiovascular',
);
$posts = get_posts( $args );
$choices = array();
foreach ( $posts as $post ) {
$choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select Procedure';
$field->choices = $choices;
}
return $form;
}
如果我明确列出术语(在上面的示例中我使用了 'cardiovascular'),第二个动态填充的字段会根据 $args 成功地提取 post 标题的过滤列表。我想知道是否有一种方法可以获取前一个字段的值并使用它来过滤第二个字段的结果(无需重新加载页面)。有任何想法吗? Gravity Forms 是否内置了这样的功能?
使用此方法,您需要使用多个页面并将第二个下拉字段添加到表单的第二个页面。然后,当用户提交第一个页面时,您可以从 $_POST 访问第一个下拉列表的值。 Gravity Forms 有一个名为 rgpost()
的辅助方法来执行此操作。您的 $args
可能是这样的:
$args = array(
'post_status' => 'publish',
'post_type' => 'procedure',
'procedure_category' => rgpost( 'input_FIELDID' ),
);
将 FIELDID 替换为您的第一个下拉列表的字段 ID。
话虽如此,如果您想在不接触 任何代码 的情况下完成此操作,请尝试 Gravity Forms Populate Anything。
https://gravitywiz.com/documentation/gravity-forms-populate-anything/
我正在尝试在 Gravity Forms 表单中动态填充两个下拉字段。第一个字段动态填充自定义 post 类型中可用的术语。我希望第二个动态填充的字段包含自定义 post 类型中所有 post 标题的列表,并让这些标题按上一个下拉列表中选择的术语进行过滤。是否可以在 Gravity Forms 中获取下拉列表的值并将该值作为参数传递给 $args 以使用 get_posts($args) 函数?
我开始使用以下教程作为指南。 https://docs.gravityforms.com/dynamically-populating-drop-down-fields/
add_filter( 'gform_pre_render_3', 'populate_procedures' );
add_filter( 'gform_pre_validation_3', 'populate_procedures' );
add_filter( 'gform_pre_submission_filter_3', 'populate_procedures' );
add_filter( 'gform_admin_pre_render_3', 'populate_procedures' );
function populate_procedures( $form ) {
// Procedure Category Dropdown
foreach ( $form['fields'] as &$field ) {
第一个字段。以下代码填充一个下拉字段,其中包含自定义 post 类型(过程)中所有术语的列表:
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate_procedure_categories' ) === false ) {
continue;
}
$terms = get_terms( array(
'taxonomy' => 'procedure_category',
'orderby' => 'name',
'order' => 'ASC',
) );
// you can add additional parameters here to alter the posts that are retrieved
// more info: http://codex.wordpress.org/Template_Tags/get_posts
//$posts = get_posts( 'post_type=procedure&numberposts=-1&post_status=publish' );
$choices = array();
foreach ( $terms as $term ) {
$choices[] = array( 'text' => $term->name, 'value' => $term->name );
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select Procedure Category';
$field->choices = $choices;
第二个字段。 以下代码使用自定义 post 类型(过程)的所有 post 标题动态填充该字段。我想根据上面选择的值过滤这些结果。
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate_procedures' ) === false ) {
continue;
}
$args = array(
'post_status' => 'publish',
'post_type' => 'procedure',
'procedure_category' => 'cardiovascular',
);
$posts = get_posts( $args );
$choices = array();
foreach ( $posts as $post ) {
$choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select Procedure';
$field->choices = $choices;
}
return $form;
}
如果我明确列出术语(在上面的示例中我使用了 'cardiovascular'),第二个动态填充的字段会根据 $args 成功地提取 post 标题的过滤列表。我想知道是否有一种方法可以获取前一个字段的值并使用它来过滤第二个字段的结果(无需重新加载页面)。有任何想法吗? Gravity Forms 是否内置了这样的功能?
使用此方法,您需要使用多个页面并将第二个下拉字段添加到表单的第二个页面。然后,当用户提交第一个页面时,您可以从 $_POST 访问第一个下拉列表的值。 Gravity Forms 有一个名为 rgpost()
的辅助方法来执行此操作。您的 $args
可能是这样的:
$args = array(
'post_status' => 'publish',
'post_type' => 'procedure',
'procedure_category' => rgpost( 'input_FIELDID' ),
);
将 FIELDID 替换为您的第一个下拉列表的字段 ID。
话虽如此,如果您想在不接触 任何代码 的情况下完成此操作,请尝试 Gravity Forms Populate Anything。
https://gravitywiz.com/documentation/gravity-forms-populate-anything/