Tạo Trường(field) repeater trong plugin Advanced Custom Field Pro(ACF)
Tạo hàm nhập liệu từ file CSV
Trong file functions.php bạn thêm đoạn code sau:
add_action( 'save_post', 'tmdev_import_data_from_csv', 10, 3 );
function tmdev_import_data_from_csv( $post_id, $post, $update ){
if( $post->post_type != 'your_custom_post_type' ) return;
if( get_field('data_results_by_provinces_vn', $post_id) ) {
// load csv with SERVER PATH instead of URL
$csv = get_attached_file(get_field('data_results_by_provinces_vn', $post_id)['id']);
if( ($handle = fopen($csv, "r") ) !== FALSE) {
$count = 0;
while(($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$count++;
update_sub_field(array('results_by_provinces_vn', $count, 'stt'), $data[0], $post_id );
update_sub_field(array('results_by_provinces_vn', $count, 'provinces_of_vn'), $data[1], $post_id );
update_sub_field(array('results_by_provinces_vn', $count, 'registered_members'), $data[2], $post_id );
update_sub_field(array('results_by_provinces_vn', $count, 'distance'), $data[3], $post_id );
}
update_post_meta( $post_id, 'results_by_provinces_vn', $count );
fclose($handle);
}
}
}