我使用以下代码将标题添加到重力形式的复选框字段中。
我需要使用不同的帖子类别填充相同表单中的多个复选框字段。
以下代码用于填充字段。但是,字段ID 8填充了5 - 7年类别以及它自己指定的类别+这也继续使用字段9。
//NOTE: update the '12' to the ID of your form
add_filter( 'gform_pre_render_12', 'populate_checkbox' );
add_filter( 'gform_pre_validation_12', 'populate_checkbox' );
add_filter( 'gform_pre_submission_filter_12', 'populate_checkbox' );
add_filter( 'gform_admin_pre_render_12', 'populate_checkbox' );
function populate_checkbox( $form ) {
foreach( $form['fields'] as &$field ) {
//NOTE: replace 4 with your checkbox field id
if ( !in_array( $field->id, array( 4, 8, 9 ) ) ) {
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)
switch( $field->id ) {
case 4:
$posts = get_posts( 'numberposts=-1&post_status=any&category_name=5-7 Years' );
break;
case 8:
$posts = get_posts( 'numberposts=-1&post_status=any&category_name=8-11 Years' );
break;
case 9:
$posts = get_posts( 'numberposts=-1&post_status=any&category_name=Adults' );
break;
}
$input_id = 1;
foreach( $posts as $post ) {
//skipping index that are multiples of 10 (multiples of 10 create problems as the input IDs)
if ( $input_id % 10 == 0 ) {
$input_id++;
}
$choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
$inputs[] = array( 'label' => $post->post_title, 'id' => "{$field_id}.{$input_id}" );
$input_id++;
}
$field->choices = $choices;
$field->inputs = $inputs;
}
return $form;
}
以下是一些屏幕截图,用于说明发生了什么以及我想要做什么:
http://carlyspicer.com/wp-content/uploads/2016/10/gravity-update-screenshot.jpg
http://carlyspicer.com/wp-content/uploads/2016/10/gravity-update-screenshot-2.jpg
你有什么建议可以解决这个问题吗?
提前感谢您的帮助!