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
  • you are erroneously using the php filter coding mechanism. Every 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
Add a comment  | 

1 Answer 1

Reset to default 1

Try 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;
}

本文标签: