admin管理员组文章数量:1334177
I recently found solution for populating dropdown list with terms relative to the post. Everything works but I need to use this code on multiple dropdown lists within one form.
My code looks something like this but it doesn’t work, can you help me with that? Thank you.
add_filter('cf7sg_dynamic_dropdown_custom_options', 'filter_options',10,3);
function filter_options($options, $field_name, $form_key){
if($form_key != 'bez-nazvu') return $options; //check this is the correct form.
if($field_name != 'dynamic_select-461') return $options; //check this is the correct field.
$options = array();
//get your terms
$terms = get_the_terms( $post->ID , 'strava' );
if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {
foreach( $terms as $term ) {
//this is the <option value="">label</opton> value->label pairs.
$options[$term->name] = $term->name;
}
} return $options;
}
function filter_options($options, $field_name, $form_key){
if($form_key != 'bez-nazvu') return $options; //check this is the correct form.
if($field_name != 'dynamic_select-462') return $options; //check this is the correct field.
$options = array();
//get your terms
$terms = get_the_terms( $post->ID , 'doprava' );
if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {
foreach( $terms as $term ) {
//this is the <option value="">label</opton> value->label pairs.
$options[$term->name] = $term->name;
}
} return $options;
}
function filter_options($options, $field_name, $form_key){
if($form_key != 'bez-nazvu') return $options; //check this is the correct form.
if($field_name != 'dynamic_select-463') return $options; //check this is the correct field.
$options = array();
//get your terms
$terms = get_the_terms( $post->ID , 'terminy' );
if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {
foreach( $terms as $term ) {
//this is the <option value="">label</opton> value->label pairs.
$options[$term->name] = $term->name;
}
} return $options;
}
I recently found solution for populating dropdown list with terms relative to the post. Everything works but I need to use this code on multiple dropdown lists within one form.
My code looks something like this but it doesn’t work, can you help me with that? Thank you.
add_filter('cf7sg_dynamic_dropdown_custom_options', 'filter_options',10,3);
function filter_options($options, $field_name, $form_key){
if($form_key != 'bez-nazvu') return $options; //check this is the correct form.
if($field_name != 'dynamic_select-461') return $options; //check this is the correct field.
$options = array();
//get your terms
$terms = get_the_terms( $post->ID , 'strava' );
if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {
foreach( $terms as $term ) {
//this is the <option value="">label</opton> value->label pairs.
$options[$term->name] = $term->name;
}
} return $options;
}
function filter_options($options, $field_name, $form_key){
if($form_key != 'bez-nazvu') return $options; //check this is the correct form.
if($field_name != 'dynamic_select-462') return $options; //check this is the correct field.
$options = array();
//get your terms
$terms = get_the_terms( $post->ID , 'doprava' );
if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {
foreach( $terms as $term ) {
//this is the <option value="">label</opton> value->label pairs.
$options[$term->name] = $term->name;
}
} return $options;
}
function filter_options($options, $field_name, $form_key){
if($form_key != 'bez-nazvu') return $options; //check this is the correct form.
if($field_name != 'dynamic_select-463') return $options; //check this is the correct field.
$options = array();
//get your terms
$terms = get_the_terms( $post->ID , 'terminy' );
if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {
foreach( $terms as $term ) {
//this is the <option value="">label</opton> value->label pairs.
$options[$term->name] = $term->name;
}
} return $options;
}
Share
Improve this question
edited Jun 29, 2020 at 16:12
Aurovrata
1,34611 silver badges18 bronze badges
asked Jun 29, 2020 at 5:35
bld23232bld23232
32 bronze badges
1
|
1 Answer
Reset to default 1Try this instead, combine all 3 functions into a single one,
add_filter('cf7sg_dynamic_dropdown_custom_options', 'filter_options',10,3);
function filter_options($options, $field_name, $form_key){
//field 'dynamic_select-461'
if($form_key == 'bez-nazvu' && $field_name == 'dynamic_select-461') {
$options = array();
//get your terms
$terms = get_the_terms( $post->ID , 'strava' );
if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {
foreach( $terms as $term ) {
//this is the <option value="">label</opton> value->label pairs.
$options[$term->name] = $term->name;
}
}
}
//field 'dynamic_select-462'
if($form_key == 'bez-nazvu' && $field_name == 'dynamic_select-462') {
$options = array();
//get your terms
$terms = get_the_terms( $post->ID , 'doprava' );
if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {
foreach( $terms as $term ) {
//this is the <option value="">label</opton> value->label pairs.
$options[$term->name] = $term->name;
}
}
}
//field 'dynamic_select-463'
if($form_key == 'bez-nazvu' && $field_name == 'dynamic_select-463') {
$options = array();
//get your terms
$terms = get_the_terms( $post->ID , 'terminy' );
if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {
foreach( $terms as $term ) {
//this is the <option value="">label</opton> value->label pairs.
$options[$term->name] = $term->name;
}
}
}
return $options;
}
本文标签:
版权声明:本文标题:select - Filter multiple dynamic dropdown lists within one form using the Smart Grid-layout extension for Contact Form 7 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742308769a2450445.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
add_filter
hook must call a single and unique function, you are trying to call 3 functions! Please read this tutorial on how to use php hooks. – Aurovrata Commented Jun 29, 2020 at 7:09