我正试图从Instagram和Twitter中提取信息,并将信息显示为屏幕上排列的方形和矩形框的集合。
我使用foreach语句来显示内容,但由于包含的div在大小上不一致,我必须结束foreach语句并开始一个新语句。新foreach的内容与之前的内容完全相同。我不确定我是否会以正确的方式解决这个问题,并希望能够朝着正确的方向努力。
下面的代码块显示前4张最新的Instagram照片。
<?php $i = 0; foreach ($instagram_data->data as $latest_post): if (++$i == 5) break; ?>
<div class="engage-block"><img src="<?= $latest_post->images->standard_resolution->url ?>"></div>
<?php endforeach ?>
之后,我会显示最新的Twitter内容。
<div class="engage-horizontal engage-block"><span>"<?php echo $latest_tweet->text ?>"</span></div>
然后我重复另一个与第一个类似的foreach来显示更多的Instagram内容。然而,这反复从上面的代码完全相同的内容(显示最新的4张照片,而不是原始照片后的4张照片)。
答案 0 :(得分:0)
你可以在foreach中放一个变量来制作twitter div,如果它是第五个,就像这样
<?php
$i = 0; foreach ($instagram_data->data as $latest_post){
?>
if ($i==5){?>
<div class="engage-horizontal engage-block"><span>"<?php echo $latest_tweet->text ?>"</span></div>
$i=0;
}
else{?>
<div class="engage-block"><img src="<?= $latest_post->images->standard_resolution->url ?>"></div>
<?php
}
$i++;
} ?>