Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用PHP显示前4行中的字段?_Javascript_Php_Wordpress_Advanced Custom Fields - Fatal编程技术网

Javascript 如何使用PHP显示前4行中的字段?

Javascript 如何使用PHP显示前4行中的字段?,javascript,php,wordpress,advanced-custom-fields,Javascript,Php,Wordpress,Advanced Custom Fields,所以我使用的是ACF(高级自定义字段),我想显示前四行。虽然总共有20行,但我只需要显示前5行或后5行中的字段 而且,我的代码不需要在循环中,所以我愿意接受任何答案。我只将代码放在循环中,因为这是我知道如何显示repeater子字段的唯一方法。我对使用ACF字段非常陌生 <?php // check if the repeater field has rows of data if( have_rows('repeat_field') ): // loop

所以我使用的是ACF(高级自定义字段),我想显示前四行。虽然总共有20行,但我只需要显示前5行或后5行中的字段

而且,我的代码不需要在循环中,所以我愿意接受任何答案。我只将代码放在循环中,因为这是我知道如何显示repeater子字段的唯一方法。我对使用ACF字段非常陌生

<?php

    // check if the repeater field has rows of data

    if( have_rows('repeat_field') ):

      // loop through the rows of data
        while ( have_rows('repeat_field') ) : the_row();

            // display a sub field value

              the_sub_field('place_name');
              the_sub_field('place_date');
              the_sub_field('place_time');


        endwhile;

    else :

        // no rows found

    endif;

在完成所需的运行次数后,只需停止循环即可:

if( have_rows('repeat_field') ):

    $i=0; // Counter to keep track of number of runs

    while ( have_rows('repeat_field') ) : the_row();
            the_sub_field('place_name');
            the_sub_field('place_date');
            the_sub_field('place_time');

            $i++; // Increase the number by one after each run
            if($i==4) break; // Stop after four runs

    endwhile;
endif;