Code repeater field acf tạo vòng lặp nhiều field
5 tháng trước,
Code theme wordpress,
58 Lượt xem
Link tham khảo: https://www.advancedcustomfields.com/resources/repeater/
Đây là code demo netweb dùng để tạo trang download tài liệu cho khách hàng.
<?php the_content(); ?> <table width="100%" border="0" align="center" cellpadding="5" cellspacing="0" class="tb_content"> <tbody> <?php if(get_field('nhieu_link_tai')): ?> <tr> <th width="70%" align="center">Tên tài liệu</th> <th width="20%" align="center">Download</th> </tr> <?php while(has_sub_field('nhieu_link_tai')): ?> <tr> <td align="left"><?php the_sub_field('ten_file_tai_lieu'); ?></td> <td align="center"><a href="<?php the_sub_field('link_download'); ?>" target="_blank" ><img src="<?php echo get_template_directory_uri(); ?>/img/download.png" alt="<?php the_sub_field('ten_file_tai_lieu'); ?>" title="<?php the_sub_field('ten_file_tai_lieu'); ?>"></a></td> </tr> <?php endwhile; ?> <?php endif; ?> </tbody> </table>
Code phân trang Custom Fields Repeater
<!-- Code phân trang Custom Fields Repeater --> test phan trang <br> <?php /* * Paginate Advanced Custom Field repeater */ if (get_query_var('page')) { $page = get_query_var('page'); } else { $page = 1; } // Variables $row = 0; $sp_per_page = 3; // How many images to display on each page $sp = get_field('nhieu_danh_muc_sp'); $total = count($sp); $pages = ceil($total / $sp_per_page); $min = (($page * $sp_per_page) - $sp_per_page) + 1; $max = ($min + $sp_per_page) - 1; // ACF Loop if (have_rows('nhieu_danh_muc_sp')) : ?> <?php while (have_rows('nhieu_danh_muc_sp')) : the_row(); $row++; // Ignore this image if $row is lower than $min if ($row < $min) { continue; } // Stop loop completely if $row is higher than $max if ($row > $max) { break; } ?> tieu de san pham <br> <!-- sub field chạy vòng lặp --> <?php the_sub_field('title_danh_muc_sp'); ?> <?php endwhile; // Pagination echo paginate_links(array( 'base' => get_permalink() . '%#%' . '/', 'format' => '?page=%#%', 'current' => $page, 'total' => $pages )); ?> <?php else : ?> No Sản phẩm found <?php endif; ?>