I have a home page with random content but I'm hoping to simplify my code

So I have a home page with random content coming from some grandChildren.

I’m wondering if there is a way to simplify the code or use a controller maybe to have this content fill the page in this layout but not have any of the ‘projects’ repeat.

Any help or point in the right direction would be most appreciated!

<div class="mt-24">
<ul class="work mt-24 pb-24">
    <?php foreach(page('work')->grandChildren()->shuffle()->limit(2) as $project): ?>
    <li>
        <figure>
            <a href=" <?= $project->url() ?> ">
                <?= $project->image()->crop(500, 300) ?>
                <div class="post-info relative">
                    <figcaption class="ml-4">
                        <h2><?= $project->title() ?></h2>
                        <p class="project-tag">
                        <?php foreach ($project->tags() as $category): ?>
                            <?= $category ?>
                        <?php endforeach ?>
                        </p>
                    </figcaption>
                </div>
            </a>
        </figure>
    </li>
    <?php endforeach ?>
</ul>
</div>

<div class="featured pb-24">
  <?php foreach(page('work')->grandChildren()->shuffle()->limit(1) as $featured): ?>
<li>
  <figure>
      <a href=" <?= $featured->url() ?> ">
          <?= $featured->image()->crop(700, 400) ?>
      </a>
      <div class="post-info relative">
          <figcaption class="ml-4">
              <h2><?= $featured->title() ?></h2>
              <p class="project-tag">
              <?php foreach ($featured->tags() as $category): ?>
                  <?= $category ?>
              <?php endforeach ?>
              </p>
          </figcaption>
      </div>
  </figure>
</li>
<div class="featured-text ml-16 mr-10 mt-24">
  <?= $featured->text() ?>
  <a href=" <?= $featured->url() ?> ">
    <button class="featured-button mt-8">View Project <i class="ml-4" data-feather="arrow-up-right"></i> </button>
  </a>
</div>
  <?php endforeach ?>
</div>
<?php
$randomProjects = page('work')->grandChildren()->shuffle()->limit(2);
foreach ( $randomProjects as $project ) : ?>
<?php /* stuff here */ ?>
<?php endforeach; ?>

Don’t know why you’re using a loop for the featured project if you want only one:

<?php
$featured = page('work')->grandChildren()->not($randomProjects)->shuffle()->limit(1);
if ($featured) : ?>
<div class="featured pb-24">
  <figure>
      <a href=" <?= $featured->url() ?> ">
          <?= $featured->image()->crop(700, 400) ?>
      </a>
      <div class="post-info relative">
          <figcaption class="ml-4">
              <h2><?= $featured->title() ?></h2>
              <p class="project-tag">
              <?php foreach ($featured->tags() as $category): ?>
                  <?= $category ?>
              <?php endforeach ?>
              </p>
          </figcaption>
      </div>
  </figure>
<div class="featured-text ml-16 mr-10 mt-24">
  <?= $featured->text() ?>
  <a href=" <?= $featured->url() ?> ">
    <button class="featured-button mt-8">View Project <i class="ml-4" data-feather="arrow-up-right"></i> </button>
  </a>
</div>
</div>
<?php endif; ?>

Thanks!

This worked with some adjustments I was just using limit() for testing but as I build out the page I want to have flexibility.

This opened my eyes. I was completely forgetting about not().

Thank you.