有没有办法在使用Wordpress高级自定义字段(ACF)的特定行上启动?
问题描述:
所以我在Wordpress中使用ACF(高级自定义字段)。我有大约40行显示,但我想从第5行开始。基本上我想显示第5行到第40行,而不是从第一行开始。有没有办法做到这一点?我的代码如下。有没有办法在使用Wordpress高级自定义字段(ACF)的特定行上启动?
<?php
if(have_rows('repeat_field')):
$i = 0;
// loop through the rows of data
while (have_rows('repeat_field')) : the_row();
$i++;
if (!empty(get_sub_field('feature_image_post'))) {
the_sub_field('feature_article_link');
the_sub_field('feature_image_post');
the_sub_field('feature_title');
}
if($i > 40)
{
break;
}
endwhile;
else :
// no rows found
endif;
?>
答
你应该使用继续在循环语句跳过迭代如下:
<?php
if(have_rows('repeat_field')):
$i = 0;
// loop through the rows of data
while (have_rows('repeat_field')) : the_row();
$i++;
if($i<5)
continue;
if (!empty(get_sub_field('feature_image_post')))
{
the_sub_field('feature_article_link');
the_sub_field('feature_image_post');
the_sub_field('feature_title');
}
if($i > 40)
{
break;
}
endwhile;
else :
// no rows found
endif;
?>
答
你应该能够做到这一点使用这样的代码:
<?php
$repeat = get_field('repeat_field');
for ($i = 5; $i <= 40; $i++) {
if (!empty($repeat[$i]['feature_image_post'])) {
echo $repeat[$i]['feature_article_link'];
echo $repeat[$i]['feature_image_post'];
echo $repeat[$i]['feature_title'];
}
}
?>
只需使用get_field
,让您的中继器的阵列,然后通过5使用for
函数在PHP中环路40
谢谢。这正是我正在寻找的! – Mariton
不客气! –