【发布时间】:2020-02-28 02:20:28
【问题描述】:
我正在尝试在重力形式表单中动态填充两个下拉字段。第一个字段动态填充自定义帖子类型中可用的术语。我希望第二个动态填充的字段包含自定义帖子类型中所有帖子标题的列表,并让这些标题按上一个下拉列表中选择的术语过滤。是否可以在 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 ) {
第一个字段。以下代码填充一个下拉字段,其中包含自定义帖子类型(过程)中所有术语的列表:
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;
第二个字段。以下代码使用自定义帖子类型(过程)的所有帖子标题动态填充该字段。我想根据上面选择的值过滤这些结果。
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;
}
如果我明确列出了该术语(在上面的示例中我使用了“心血管”),则第二个动态填充的字段会根据 $args 成功提取过滤后的帖子标题列表。我想知道是否有一种方法可以获取前一个字段的值并使用它来过滤第二个字段的结果(无需重新加载页面)。有任何想法吗? Gravity Forms 是否内置了这样的功能?
【问题讨论】:
标签: wordpress gravity-forms-plugin gravityforms