I’m developing a site that has multiple sections, and I need to make an alteration to the first and last section of a foreach loop.
I followed the cookbook here and have this:
<div class="main-content"> <?php if ($projects = page('projects')) : ?> <?php $first = $projects->first(); ?> <?php foreach ($projects->children()->listed() as $project) : ?> <?php if ($project = =$first) : ?> <?php snippet('firstAlbum', [ 'project' => $project ]) ?> <?php else : ?> <?php snippet('album', [ 'project' => $project ]) ?> <?php endif ?> <?php endforeach ?> <?php endif ?> </div>
Where I’m creating a variable that is equal to the first project in the loop, and testing if the project being looped over is that one. However, this doesn’t work, it just treats the project as if it is not the first and uses the album template. While I know this question has been asked before here the eventual solution was a bit hacky, and I’m curious as to why the solution in the docs doesn’t work.
Thanks in advance!