2016-11-11 142 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');