我在这个自定义重力表格中有两个选择字段,我一直在努力解决。第一个字段是网络研讨会列表。第二个字段是网络研讨会日期的列表。显示哪些日期取决于您选择的网络研讨会。我计划使用 jQuery 对该列表进行排序,但为了做到这一点,我需要向网络研讨会日期选择字段中的选择选项添加一个属性。目前我不知道如何修改GF的select options功能。这就是我的自定义函数目前的样子:
//Populate Webinars form field for Webinar dates
add_filter( 'gform_pre_render_2', 'populate_webinar_dates' );
function populate_webinar_dates( $form ) {
foreach( $form[ 'fields' ] as &$field ) {
if( $field[ 'type' ] != 'select' || strpos( $field[ 'cssClass' ], 'populate-dates' ) === false )
continue;
$posts = new WP_Query( 'numberposts=-1&post_status=publish&post_type=vtl_webinar' );
$choices = '<option value=" ">Select a date</option>';
while ( $posts->have_posts() ) : $posts->the_post();
while ( has_sub_field( 'dates_available' ) ) :
$post_dates = array( 'date' => get_sub_field( 'date' ) );
$post_title = array( 'name' => str_replace( " ", "-", get_the_title() ) );
//$choices[] = array( 'text' => $post_dates['date'], 'value' => $post_dates['date'] );
$choices = '<option value="' . $post_dates['date'] . '" data-id="' . $post_title . '">' . $post_dates['date'] . '</option>';
endwhile;
endwhile;
$field[ 'choices' ] = $choices;
}
return $form;
}
显然,这条线不起作用:
$choices = '<option value="' . $post_dates['date'] . '" data-id="' . $post_title . '">' . $post_dates['date'] . '</option>';
它期望的参数类似于该行上方的注释形式,但您不能只将自己的属性添加到该行。您必须根据 GF 正在寻找的一组现有参数进行工作。有人可以帮我解决这个问题吗?我只需要获取选项中的数据属性即可。
我的做法是错误的。为了获取我尝试创建的自定义选项,我使用了以下代码而不是上面发布的代码。
add_filter("gform_field_input", "webinar_date_option", 10, 2);
function webinar_date_option($input, $field, $value, $lead_id, $form_id) {
if ( $field["cssClass"] == "populate-dates" ) {
$input = '<select name="input_2" id="input_2_2" class="medium gfield_select" tabindex="2">';
$posts = new WP_Query( 'numberposts=-1&post_status=publish&post_type=vtl_webinar' );
$input .= '<option value=" ">Select Date</option>';
while ( $posts->have_posts() ) : $posts->the_post();
while ( has_sub_field( 'dates_available' ) ) :
$post_dates = array( 'date' => get_sub_field( 'date' ) );
$date_id = array( 'id' => get_the_ID() );
$input .= '<option value="'. $post_dates['date'] .'" data-id="'. $date_id['id'] .'">'. $post_dates['date'] .'</option>';
endwhile;
endwhile;
$input .= '</select>';
}
return $input;
}