2016-11-11 143 views
0

我使用的是ACF tutorial here to build from. 我想要做的是使用文本子字段中的值填充其他选择子字段同样的中继器领域。我知道这听起来是递归的,也许这是令人望而却步的。现场管理员不会对ajax-y或即时更新,它更像是其他网站功能的管理员字段。如何自动填充子字段从父字段中选择

无论如何,这是我迄今为止。

  • ACF中继字段= core_values
  • 页的字段是= valuesadmin
  • 源文本子场core_values内=值名称
  • 目标子场( 从值名称每个需要动态地传播选择) =
    • constructor1_name
    • constructor2_name
    • constructor3_name
    • destructor1_name
    • destructor2_name

我试图修改代码,在刚才的链接教程,并把它放在主题的functions.php的,并在插件的主要文件我正在构建其他自定义函数。

/** 
* ACF population functions 
*/ 

function acf_load_core_values_field_choices($field) { 

// reset choices 
$field['choices'] = array(); 


// if has rows 
if(have_rows('core_values', 'valuesadmin')) { 

    // while has rows 
    while(have_rows('core_values', 'valuesadmin')) { 

     // instantiate row 
     the_row(); 


     // vars 
     $value = get_sub_field('value_name'); 
     $label = get_sub_field('value_name'); 


     // append to choices 
     $field['constructor1_name'][ $value ] = $label; 
     $field['constructor2_name'][ $value ] = $label; 
     $field['constructor3_name'][ $value ] = $label; 
     $field['destructor1_name'][ $value ] = $label; 
     $field['destructor2_name'][ $value ] = $label; 

    } 

} 


// return the field 
    return $field; 

} 

add_filter('acf/load_field/name=constructor1_name', 'acf_load_core_values_field_choices'); 
add_filter('acf/load_field/name=constructor2_name', 'acf_load_core_values_field_choices'); 
add_filter('acf/load_field/name=constructor3_name', 'acf_load_core_values_field_choices'); 
add_filter('acf/load_field/name=destructor1_name', 'acf_load_core_values_field_choices'); 
add_filter('acf/load_field/name=destructor2_name', 'acf_load_core_values_field_choices'); 

很明显,这不会传播选择子字段,因为我喜欢。

问题: - 这是甚至可能的(value_name字段都已填充值) - 功能代码应该去哪里? - 也许我已经破坏了代码

在此先感谢!

+0

我会尝试使用除WordPress之外的其他东西:-)。但是,如果WP-Plugin没有可帮助您加载按先前选择的值过滤的选项的构建函数,则应该尝试使用JavaScript。或者jquery.ajax函数:http://api.jquery.com/jquery.ajax/ – Oliver

回答

0

嗯,我通过首先将这一切全部移到ACF选项页面,然后创建另一个ACF字段(values_master),我可以在选项页面的第二个字段中动态填充值,从而实现了我期待的功能。所以我不确定这是否因某些递归而无法工作,但它工作正常。

function acf_load_value_field_choices($field) { 

    // reset choices 
    $field['choices'] = array(); 


    // if has rows 
    if(have_rows('values_master', 'option')) { 

     // while has rows 
     while(have_rows('values_master', 'option')) { 

      // instantiate row 
      the_row(); 


      // vars 
      $value = get_sub_field('value_name'); 
      $label = get_sub_field('value_name'); 


      // append to choices 
      $field['choices'][ $value ] = $label; 

     } 

    } 


    // return the field 
    return $field; 

} 

add_filter('acf/load_field/name=constructor1_name', 'acf_load_value_field_choices'); 
add_filter('acf/load_field/name=constructor2_name', 'acf_load_value_field_choices'); 
add_filter('acf/load_field/name=constructor3_name', 'acf_load_value_field_choices'); 
add_filter('acf/load_field/name=destructor1_name', 'acf_load_value_field_choices'); 
add_filter('acf/load_field/name=destructor2_name', 'acf_load_value_field_choices'); 
add_filter('acf/load_field/name=value_mstr_name', 'acf_load_value_field_choices');