Modifying the first item in a loop

Hey all.

I’m trying to list out a number of blog posts onto a page.

For the first link, I want to make the markup different to the rest of the items.

However, I can’t seem to specify the first item in a list to be different from the rest.

I tried this:

   <?php

      $items  = $page->grandChildren();
      $first  = $items->first();

      ?>

 <ul>
        <?php foreach($items as $item): ?>

          <?php if($item == $first) ?>
              <h1>First item</h1>

          <?php else ?>

        <li>

            <h1>Every other item</h1>
        </li>


        <?php endforeach ?>
      </ul>

However, It didn’t work.

Any help would be greatly appreciated!

Not tested but you could try:

<?php if($item->is($first)) ?>

That didn’t work :pensive:

But thanks for the super quick response!

What you could do is to completely separate the two cases:

<ul>
  <li>
     <?php $item = $page->grandChildren()->first() ?>
     <h1>First item</h1>
  </li>
  
  <?php foreach($page->grandChildren()->offset(1) as $item): ?>
    <li>
      <h1>Every other item</h1>
    </li>
  <?php endforeach ?>
</ul>

What exactly does not work in my solution? It should actually work.

BTW. You should wrap your if-statement within a li tag, not outside of it.

This worked perfectly, thanks so much!