Support

Account

Home Forums Backend Issues (wp-admin) Populate select field with directories

Helping

Populate select field with directories

  • Hello,

    I have tried numerous things, but I can’t get it to work. What I am trying to achieve is the following.

    I want to populate the select field with custom choices. Those choices are subdirectories of the directory let’s say ‘photos’. Those subdirectories are ‘2015’, ‘2014’, etc. Within those directories are different folders, which can be named anything you like.

    I have two custom select fields, one that contains the directories per year and the second the child directories of the the directory chosen in the first select field.

    Is there a change to get such a structure? I’m using php glob() function to scan the directories, that gives me an array.

  • i think it is possible when you “adapt” this dynamically-populate-a-select-fields-choices and combine it with your function to scan the directories.

    for first dropdown do something like that:

    function acf_load_color_field_choices( $field ) {
        // reset choices
        $field['choices'] = array();
    //fill here your array into the variable $choices
    $choices = php glob() function that you have;
    //
        // 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=color', 'acf_load_color_field_choices');

    for second dropdown, you need to get the value of first dropdown and do nearly same again.
    hope i have understand you right, and this answer help. or at least lead you to the right direction

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Populate select field with directories’ is closed to new replies.