admin管理员组

文章数量:1391969

I have a question. I have 2 types of custom field group: Group A and Group B. All I am using ACF. In Group A, I have field name course_trainer. This field type is a User type and can have multiple values.

In Group B, I have field name class_trainer.This field type is a Select type and cannot have multiple value. This field will display data from field course_trainer. Basically, I used dynamic select for data to display at class_trainer.

If course_trainer field type not using multiple values, I can manage to display the data but if field type can have multiple values, the data not display at all.

I create a function in function.php to display the dynamic select:

function acf_load_t_first_name2_field_choices($field) {
    global $post;
    //$post = $_GET['post'];
    // reset choices
    $field['choices'] = array();


    // get the textarea value from options page without any formatting
    $choices = get_field('t_first_name',$post->ID);



    // loop through array and add to field 'choices'
    if( is_array($choices) ) {

        foreach( $choices as $choice ) {

            $field['choices'][ $choice ] = $choice;

        }

    }


    // return the field
    return $field;

}

add_filter('acf/load_field/name=course_trainer', 'acf_load_t_first_name2_field_choices');

Can someone correct my codes? Please help me :(

I have a question. I have 2 types of custom field group: Group A and Group B. All I am using ACF. In Group A, I have field name course_trainer. This field type is a User type and can have multiple values.

In Group B, I have field name class_trainer.This field type is a Select type and cannot have multiple value. This field will display data from field course_trainer. Basically, I used dynamic select for data to display at class_trainer.

If course_trainer field type not using multiple values, I can manage to display the data but if field type can have multiple values, the data not display at all.

I create a function in function.php to display the dynamic select:

function acf_load_t_first_name2_field_choices($field) {
    global $post;
    //$post = $_GET['post'];
    // reset choices
    $field['choices'] = array();


    // get the textarea value from options page without any formatting
    $choices = get_field('t_first_name',$post->ID);



    // loop through array and add to field 'choices'
    if( is_array($choices) ) {

        foreach( $choices as $choice ) {

            $field['choices'][ $choice ] = $choice;

        }

    }


    // return the field
    return $field;

}

add_filter('acf/load_field/name=course_trainer', 'acf_load_t_first_name2_field_choices');

Can someone correct my codes? Please help me :(

Share Improve this question edited Mar 4, 2020 at 8:50 user3239674 asked Mar 4, 2020 at 7:22 user3239674user3239674 134 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Can you try this amendment and see if $field is returning as you expect?

$field['choices'][] = $choice;

本文标签: advanced custom fieldsACF Dynamic select not showing data