我正在尝试用自定义帖子类型“color”的数组填充下拉字段
使用此:https://www.gravityhelp.com/documentation/article/dynamically-populating-drop-down-fields/
add_filter( \'gform_pre_render_51\', \'color_posts\' );
add_filter( \'gform_pre_validation_51\', \'color_posts\' );
add_filter( \'gform_pre_submission_filter_51\', \'color_posts\' );
add_filter( \'gform_admin_pre_render_51\', \'color_posts\' );
function color_posts( $form ) {
foreach ( $form[\'fields\'] as &$field ) {
if ( $field->type != \'select\' || strpos( $field->cssClass, \'color_posts\' ) === false ) {
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)
$args = array( \'post_type\' => \'color\' );
$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 a Post\';
$field->choices = $choices;
}
return $form;
}
但即使我将自定义css样式设置为“color\\u posts”,也不会发生任何事情。任何我做错的事情,或者我可以用不同的方式来做。
最合适的回答,由SO网友:IAmDhar 整理而成
表单的id应该是51,以便代码正常工作,希望代码位于函数中。php
对于gform\\u pre\\u render\\u 51,51是您试图修改其选择字段的表单的id