Code repeater field acf tạo vòng lặp nhiều field
4 năm trước,
Code theme wordpress,
1213 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>
Nội dung
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; ?>
Bộ lặp ngẫu nhiên / xáo trộn với vòng lặp nâng cao
các bạn có thể tham khảo đoạn code này chèn vào file funcion
// Function to randomize Advanced Custome Fields' Repeaters
function my_acf_load_value3( $value, $post_id, $field )
{
shuffle($value);
return $value;
}
// Randomize ACF menu_kho_giao_dien1 Repeater
add_filter('acf/load_value/name=menu_kho_giao_dien1', 'my_acf_load_value3', 10, 3);
tham khảo: https://support.advancedcustomfields.com/forums/topic/randomizingshuffling-repeater-with-advanced-loop/
Bài viết mới cập nhập
Chủ đề liên quan theo Tag





Bình luận trên facebook netweb.vn