Nội dung

Code repeater field acf tạo vòng lặp nhiều field

3 năm trước, Code theme wordpress, 1077 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; ?>

 

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

  • Hướng dẫn Plugin floating contact

    Hướng dẫn Plugin floating contact

    1 năm trước, 380 Lượt xem

    Netweb xin hướng dẫn cách quản lý Plugin floating contact các bạn nhớ làm theo kỹ tường bước để plugin hoạt động Tính năng: Hiển…

  • Backup Yandex Mail

    Backup Yandex Mail

    1 năm trước, 251 Lượt xem

    Khác với dịch vụ email theo tên của microsoft và google với chi phí đắt đỏ tầm 1tr/năm/1user, vì thế nên netweb khuyên dùng dịch…

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

Chủ đề liên quan theo Tag

Đam mê thiết kế web, kinh nghiệm 9 năm trong nghề thiết kế website, đang thiết kế web dạo tại netweb.vn, Callme: 08.9898.2526 (Mr Hải)