
Lấy data cuối trong repeater field – Advanced Custom Fields (ACF)
Trong quá trình code website và sử dụng plugin ACF | Advanced Custom Fields Plugin for WordPress mình rất hay lấy dữ liệu cuối cùng của repeat field nên viết lên đây để note và share cho những ai mới làm web cần sử dụng.
<?php if (have_rows('subplaats')): $all_fields_count = count(get_field('subplaats')); $fields_count = 1; ?> <div class="subplaats-container"> <?php while (have_rows('subplaats')): the_row(); $plaats = get_sub_field('plaats'); echo $plaats; if ($fields_count == $all_fields_count) { echo "Field cuối"; } else { echo "Field theo thứ tự mặc định"; } $fields_count++; endwhile; ?> </div> <?php endif; ?>
Đoạn code trên bạn đọc sẽ hiểu ngay thôi, ban đầu đặt field $fields_count = 1
mỗi 1 lần lấy dữ liệu ra khi dùng vòng lặp while sẽ cho $fields_count tăng lên 1 lần, $all_fields_count
sẽ dùng hàm count
để đếm toàn bộ row repeat field, $fields_count
sẽ bằng $all_fields_count
thì đó sẽ là row cuối hay field cuối trong repeat field trong ACF
Chúc bạn thành công.
Nguồn: stackoverflow