ACF将数据库中的选择填充到后端的选择框中

时间:2017-01-08 05:13:10

标签: wordpress advanced-custom-fields

如何从数据库中填充选择而不是静态?见附件:

enter image description here

1 个答案:

答案 0 :(得分:0)

我把这个函数放到了我的function.php中,所以我只需要在custum表中添加我的选项,例如像国家一样。

function makeChoiceArrayForACF($postid){
    $datafromdbase = someFunctionToGetDataFromDbase();
    $newdbasearray = array();
    foreach ($datafromdbase as $key => $value){
        $newdbasearray[$key] = $key;
    }
    $new = array(
        'multiple' => 0,
        'allow_null' => 1,
        'choices' =>
            $newdbasearray,
        'default_value' =>
            array(),
        'ui' => 0,
        'ajax' => 0,
        'placeholder' => '',
        'return_format' => 'array',
        'type' => 'select',
        'instructions' => '',
        'required' => 1,
        'conditional_logic' => 0,
        'wrapper' =>
            array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
    );
    $my_post = array(
        'ID'           => $postid,
        'post_content' => serialize($new),
    );
    wp_update_post( $my_post );
}

add_action('init', 'admin_only');

function admin_only() {
    if( !is_admin() )
        return;
    makeChoiceArrayForACF(16611);
}