Featured / Non Featured Portfolio Pieces

Hi, I have set up blog posts fine to show featured and non featured but I can seem to get the Portfolio posts to do the same. I’m probably doing something stupid simple wrong but if anyone can point out where I’m going wrong that would be great.

This is the current code without my additional code (due to my dodgy code)

<?php snippet('head') ?>

<div class="container <?= $page->slug() ?>" >
  <div class="module nopad">
    <?php snippet('header') ?>
    <main>
      <header class="intro">
        <h1><?php echo $page->title()->html() ?></h1>
      </header>
      <?php echo $page->text()->kt() ?>

      <div class="portfolioslist">

      <!-- FEATURED PORTFOLIO PIECES -->
      <?php foreach($page->children()->visible()->flip() as $portfolio): ?>
        <section>
          <div class="imgcontainer">
          <a href="<?php echo $portfolio->url() ?>" class="title-link">
            <img src="<?= $portfolio->featuredimage()->toFile()->url() ?>" alt="<?= $portfolio->title() ?>"></a>
          </div>
          <div class="textcontainer">
            <h2><a href="<?php echo $portfolio->url() ?>" class="title-link"><?php echo $portfolio->title()->html() ?></a></h2>
            <p><?php echo $portfolio->text()->excerpt(300) ?></p>
            <p class="button"><a href="<?php echo $portfolio->url() ?>" class="readmore button">Read more</a></p>
          </div>
        </section>
      <?php endforeach ?>

      <hr />
      
      <!-- NON FEATURED PORTFOLIO PIECES -->
      <?php foreach($page->children()->visible()->flip() as $portfolio): ?>
        <section>
          <div class="imgcontainer">
          <a href="<?php echo $portfolio->url() ?>" class="title-link">
            <img src="<?= $portfolio->featuredimage()->toFile()->url() ?>" alt="<?= $portfolio->title() ?>"></a>
          </div>
          <div class="textcontainer">
            <h2><a href="<?php echo $portfolio->url() ?>" class="title-link"><?php echo $portfolio->title()->html() ?></a></h2>
            <p><?php echo $portfolio->text()->excerpt(300) ?></p>
            <p class="button"><a href="<?php echo $portfolio->url() ?>" class="readmore button">Read more</a></p>
          </div>
        </section>
      <?php endforeach ?>
      
      </div>
    </main>
  </div>
</div>

<?php snippet('footer') ?>

How is a featured vs not featured portfolio items defined? A toggle field? Then you have to filter your items by that field. What have you done in your blog? The code should be the same.

Just on a side note: Use listed() instead of the deprecated visible() method. It will still work but won’t have a future.

Yeah it’s just the toggle. Also thank you for the listed() info

$featuredItems = $page->children()->listed()->filterBy('name_of_toggle_field', true);
$nonFeaturedItems =  $page->children()->listed()->filterBy('name_of_toggle_field', false);

Won’t I still need the foreach though?

Yes, of course; I just defined the collections as variables you can then use in your loops:

<?php
foreach($featuredItems as $featuredItem):
// code
endforeach;

Never use code like the above, please. Always check for an object before you call the url() method

<?php if($image = $portfolio->featuredimage()->toFile()): ?>
 <a href="<?php echo $portfolio->url() ?>" class="title-link">
 <img src="<?= $image->url() ?>" alt="<?= $portfolio->title() ?>">
</a>
<?php endif ?>
2 Likes

Did the solution work for you, @Suleiman_Leadbitter, so we can close this?

@texnixe Sorry, haven’t got it working just yet.

What information is missing yet? Does the filter not work as expected? Do you get any error messages?